Skip to content

Instantly share code, notes, and snippets.

@ruffle1986
Last active October 18, 2017 16:01
Show Gist options
  • Save ruffle1986/0edfbb1959333f8ed2ac1f3d4bb48b8b to your computer and use it in GitHub Desktop.
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?
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