May 2017
Intermediate to advanced
388 pages
7h 30m
English
One can say that React barely has any API. It has about 9-10 methods and that is all we get.
The State is one of the main APIs in React. The best place to define the initial state of a component is in the class constructor.
Create a class component and initialize its state:
class Button extends React.Component { constructor(props){ super(props)this.state = {text: 'OFF'} this.handleClick = this.handleClick.bind(this)} handleClick(){this.state.text === 'OFF' ? this.setState({text: 'ON'}) :this.setState({text: 'OFF'})} render() { return ( <button type="button" onClick={this.handleClick}> {this.state.text}</button>) }}
This is an example of an internal state of a button that changes its text on user click (it toggles ...
Read now
Unlock full access