Listing 7-6: Using an inline event handler to call setState()
import {Component} from 'react';
class ScreenDoor extends Component {
constructor(props){
super(props);
this.state={
isOpen:true
}
}
render(){
return(
<button onClick={()=>this.setState({isOpen:!this.state.isOpen})}>
{this.state.isOpen?'Close the Door':'Open the Door'}
</button>
)
}
}
export default ScreenDoor;
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.