• React module methods with passing props to child or invoking callback to parent.


    Some code samples for this pupose:

    import React from "react";
    import MyDemo from "./mydemo.jsx";
    
    export default class Square extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          count: 0
        };
        this.handleChange = this.handleChange.bind(this);
        this.changeMyStateFromChild = this.changeMyStateFromChild.bind(this);
      }
      componentDidMount() {
        let me = this;
        me.setState({
          count: me.state.count + 1
        });
        console.log(me.state.count); // 打印出0
        me.setState({
          count: me.state.count + 1
        });
        console.log(me.state.count); // 打印出0
        setTimeout(function() {
          me.setState({
            count: me.state.count + 1
          });
          console.log(me.state.count); // 打印出2
        }, 0);
        setTimeout(function() {
          me.setState({
            count: me.state.count + 1
          });
          console.log(me.state.count); // 打印出3
        }, 0);
      }
      handleChange(e) {
        let me = this;
        const target = e.target;
        console.log(me);
        alert(me.state.count);
        this.setState({
          [target.name]: target.value
        });
        console.log(MyDemo);
      }
      changeMyStateFromChild(state) {
        // this.setState(state);
        debugger;
        alert(state);
      }
      render() {
        return (
          <div onChange={e => this.handleChange(e)}>
            <MyDemo
              title={this.state.count}
              changeParent={this.changeMyStateFromChild}
            />
            <input type="text" name="username" />
            <input type="text" name="password" />
            <button onClick={() => alert(MyDemo.title)}>click </button>
            <h1>{this.state.count}</h1>
          </div>
        );
      }
    }
    View Code
  • 相关阅读:
    富文本编辑器Ueditor
    记一个好用的轮播图的FlexSlider
    记一次couchbase(memcached)安装以及使用
    写了一个联动select的jquery选择器
    ios访问手机通讯录获取联系人手机号
    Swift中自定义SubString
    Swift中给UIView添加Badge
    Swift计算两个经纬度之间的球面面积
    Swift项目使用SWTableViewCell
    SQLite.Swift 中的一些用法
  • 原文地址:https://www.cnblogs.com/hualiu0/p/9027022.html
Copyright © 2020-2023  润新知