| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <!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,
- isShow: true
- }
- // 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 + 1 })
- )
- }
- 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 userInfo={this.state.name} />
- // </div >
- // )
- return <Child userInfo={this.state.name} />
- }
- }
- class Child extends React.Component {
- constructor() {
- console.log('con')
- super();
- this.state = {
- weather: "晴天"
- }
- this.handleWeather = this.handleWeather.bind(this);
- }
- // 挂载:constructor render componentDidMount
- // 更新:render componentDidUpdate
- componentDidMount() {
- console.log("挂载")
- }
- componentDidUpdate() {
- console.log("222")
- }
- componentWillUnMount() {
- console.log("卸载")
- }
- handleWeather() {
- this.setState({
- weather: "雨天"
- })
- console.log(this)
- }
- render() {
- console.log("render")
- return (
- <div>
- <h1>外面是{this.state.weather},我叫{this.props.userInfo}</h1>
- <button onClick={this.handleWeather}>修改天气</button>
- </div>
- )
- }
- }
- const element = <News />
- const container = document.getElementById("root");
- const root = ReactDOM.createRoot(container)
- root.render(element);
- </script>
- </body>
- </html>
|