|
@@ -0,0 +1,58 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>Document</title>
|
|
|
+ <script src="./babel.min.js"></script>
|
|
|
+ <script src="./react.development.js"></script>
|
|
|
+ <script src="./react-dom.development.js"></script>
|
|
|
+</head>
|
|
|
+
|
|
|
+<body>
|
|
|
+ <div id="root"></div>
|
|
|
+ <script type="text/babel">
|
|
|
+ const root = ReactDOM.createRoot(document.getElementById("root"));
|
|
|
+ function App() {
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <Count num={5} />
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ class Count extends React.Component {
|
|
|
+ constructor(props) {
|
|
|
+ super(props);
|
|
|
+ this.state = {
|
|
|
+ count: 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ handleAdd() {
|
|
|
+ // this.setState({
|
|
|
+ // count: this.state.count + 1
|
|
|
+ // })
|
|
|
+ this.setState((prevState, props) => {
|
|
|
+ console.log(prevState, props, '打印1')
|
|
|
+ return {
|
|
|
+ count: prevState.count + props.num
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ render() {
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <p>
|
|
|
+ <button onClick={this.handleAdd.bind(this)}>+</button>
|
|
|
+ {this.state.count}
|
|
|
+ <button>-</button>
|
|
|
+ </p>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ root.render(<App />);
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|