scrimba
Learn class components in React
Lifecycle methods: componentWillUnmount() 🪦
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Lifecycle methods: componentWillUnmount() 🪦
AboutCommentsNotes
Lifecycle methods: componentWillUnmount() 🪦
Expand for more info
WindowTracker.js
run
preview
console
import React from "react"

export default class WindowTracker extends React.Component {
state = {
windowWidth: window.innerWidth
}

watchWidth = () => {
this.setState({windowWidth: window.innerWidth})
}

componentDidMount() {
window.addEventListener("resize", this.watchWidth)
}

render() {
return (
<h1>Window width: {this.state.windowWidth}</h1>
)
}
}
Console
/index.html
-4:08