1
With the code just below I want to simulate the same effect with React Hook
in the componentWillMount
method contained in classes for reactjs?
Example below with code:
class Example extends React.Component {
constructor() {
super();
this.state = {
count: 0
}
this.handleIncrement
= this.handleIncrement.bind(this);
}
componentWillMount(){
console.log('Component InitMount');
}
handleIncrement() {
let { count } = this.state;
count = count + 1;
this.setState({count});
}
render() {
return (
<div>
<div>{this.state.count}</div>
<div><button onClick={this.handleIncrement}>Incremento</button></div>
</div>
);
}
}
ReactDOM.render(<Example/>, document.getElementById('root'));
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root">Aguarde ...</div>