Last active
October 18, 2017 16:01
-
-
Save ruffle1986/0edfbb1959333f8ed2ac1f3d4bb48b8b to your computer and use it in GitHub Desktop.
This program intends to increase the number of `1`s in an array and display the actual length of it in the buttons label. Does this code work as expected? If not, why?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
class MyComponent extends React.Component { | |
state = { | |
data: [] | |
} | |
handleClick = () => { | |
const data = this.state.data; | |
data.push(1); | |
this.setState({ | |
data | |
}); | |
} | |
shouldComponentUpdate(props, state) { | |
return this.state.data !== state.data; | |
} | |
render() { | |
return ( | |
<button onClick={this.handleClick}> | |
Click me! { this.state.data.length } | |
</button> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment