change setState to use a function

Also added note to readme
This commit is contained in:
Matt Pham
2019-02-27 02:37:14 -08:00
parent 119fb5785d
commit 96412d1d97
2 changed files with 6 additions and 4 deletions

View File

@@ -14,12 +14,12 @@ export class Counter extends React.Component<any, any> {
return (
<div>
{text}: {counter}
<Button onClick={this._onButtonCLick}>Click</Button>
<Button onClick={this._onButtonClick}>Click</Button>
</div>
);
}
_onButtonCLick = () => {
this.setState({ counter: this.state.counter + 1 });
_onButtonClick = () => {
this.setState((state) => ({ counter: state.counter + 1 }));
};
}