Created
February 27, 2015 10:27
-
-
Save castaneai/9ce7815aba7f1024038d to your computer and use it in GitHub Desktop.
React.js (ES6) Simple Counter
This file contains 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
class App extends React.Component { | |
constructor(props) { | |
this.state = { | |
count: 0 | |
} | |
} | |
onClick(e) { | |
this.setState({ | |
count: this.state.count + 1 | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<h1>{this.state.count}</h1> | |
<button onClick={this.onClick.bind(this)}>Count Up!!</button> | |
</div> | |
) | |
} | |
} | |
React.render( | |
<App/>, | |
document.getElementById('app-container') | |
); |
This file contains 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
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>React Test</title> | |
<script src="http://fb.me/react-with-addons-0.13.0-rc1.min.js"></script> | |
<script src="http://fb.me/JSXTransformer-0.13.0-rc1.js"></script> | |
<script src="app.jsx" type="text/jsx;harmony=true"></script> | |
<head> | |
<body> | |
<div id="app-container"></div> | |
</body> | |
</html> |
https://reactjs.org/docs/state-and-lifecycle.html
// Wrong
this.setState({
counter: this.state.counter + this.props.increment,
});
// Correct
this.setState((prevState, props) => ({
counter: prevState.counter + props.increment
}));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this program is not correct