|
|
@@ -0,0 +1,76 @@
|
|
|
+<!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">
|
|
|
+ // props
|
|
|
+ // state
|
|
|
+ class News extends React.Component {
|
|
|
+ state = {
|
|
|
+ name: '图图',
|
|
|
+ age: 3,
|
|
|
+ sum: 0
|
|
|
+ }
|
|
|
+ // handleClick = () => {
|
|
|
+ // console.log("触发")
|
|
|
+ // // this.state.age += 2;
|
|
|
+ // console.log(this)
|
|
|
+ // }
|
|
|
+ handleClick() {
|
|
|
+ this.setState({
|
|
|
+ age: this.state.age + 10,
|
|
|
+ name: 'Tom'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ handleAdd() {
|
|
|
+ this.setState((val) => ({ sum: val.sum + 10 })
|
|
|
+ )
|
|
|
+ }
|
|
|
+ handleReduce() {
|
|
|
+ this.setState({
|
|
|
+ sum: this.state.sum - 1
|
|
|
+ })
|
|
|
+ }
|
|
|
+ render() {
|
|
|
+ console.log(this, '11')
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <h1>父组件</h1>
|
|
|
+ <h1>数值:{this.state.sum}</h1>
|
|
|
+ <button onClick={this.handleAdd.bind(this)}>+1</button>
|
|
|
+ <button onClick={this.handleReduce.bind(this)}>-1</button>
|
|
|
+ <h3>我叫{this.state.name},今年{this.state.age}岁</h3>
|
|
|
+ <button onClick={this.handleClick.bind(this)}>+2</button>
|
|
|
+ <button onClick={() => {
|
|
|
+ console.log(this)
|
|
|
+ }}>+2</button>
|
|
|
+ <Child />
|
|
|
+ </div >
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ class Child extends React.Component {
|
|
|
+ render() {
|
|
|
+ return (
|
|
|
+ <div>子组件</div>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const element = <News />
|
|
|
+ const container = document.getElementById("root");
|
|
|
+ const root = ReactDOM.createRoot(container)
|
|
|
+ root.render(element);
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|