-
Notifications
You must be signed in to change notification settings - Fork 0
React Events
React makes use of basic HTML events by wrapping them in something called SyntheticEvents.
This wrapper allows React to make sure events are handled the same way across all browsers by a consistent API.
class Tickler extends React.Component {
tickle = () => {
console.log('Tee hee!')
}
render() {
return (
<button onClick={this.tickle}>Tickle me!</button>
)
}
}The handler name is always comprised of on, and the event name itself (i.e. click). These are joined together and camel-cased. As you know, the value of the events are callbacks (either a reference to a function or an inline function).
You will commonly see React component methods defined with arrow functions. This is because we often want to access the this keyword within the methods themselves.
component with multiple events:
render() {
return (
<canvas
onMouseMove={this.handleMouseMove}
onClick={() => {toggleCycling()}}
onKeyDown={this.handleKeyDown}
width='900'
height='600
tabIndex="0">
</canvas>
)
}Handling mouse coordinates with onMouseMove
handleMouseMove = (event) => {
drawChromeBoiAtCoords(event.clientX, event.clientY);
}props are dificult to change without recreating the component passing from the Parent giving the props again. For this reason for changing features in the componenet we use this.state. State is for values that are expected to change.
class MyComp extends React.Component {
// we use the constructor to set the INITIAL STATE
constructor() {
super()
this.state = {
count: 0
}
}
// our increment method makes use of the 'setState' method, which is what we use to alter state
increment = () => {
const newCount = this.state.count + 1
this.setState({
count: newCount
})
}
render() {
return (
<div onClick={this.increment}>
{this.state.count}
</div>
)
}
}We set initial state in the constructor because it runs first for React Component Lifecycle reasons.
setState() sets state asynchronously.
class App extends Component {
constructor() {
super()
this.state = {
count: 0
}
}
increment = () => {
console.log(`before setState: ${this.state.count}`)
this.setState({
count: this.state.count + 1
})
console.log(`after setState: ${this.state.count}`)
}
render() {
return (
<div onClick={this.increment}>
{this.state.count}
</div>
)
}
}
// click
//=> before setState: 0
//=> after setState: 0)
// <div>1</div>"After setState:" should have given 1 instead returned 0. Because of asyncronicity.
While component state is a very powerful feature, it should be used as sparingly as possible.
State adds (sometimes unnecessary) complexity and can be very easy to lose track of. The more state we introduce in our application, the harder it will be to keep track of all of the changes in our data.
Remember: state is for values that are expected to change during the components life.
- Mounting -
static getDerivedStateFromProps(props, state)- Mounting -
componentDidMount()- Updating -
static getDerivedStateFromProps(props, state)- Updating -
shouldComponentUpdate(nextProps, nextState)- Updating -
render()- Updating -
getSnapshotBeforeUpdate(prevProps, prevState)- Updating -
componentDidUpdate(prevProps, prevState, snapshot)- Unmounting -
componentWillUnmount()- Element Ref