美文网首页
react学2-父子组件通信

react学2-父子组件通信

作者: 木木_bfe8 | 来源:发表于2018-06-09 15:44 被阅读0次

react单项数据流动

父组件可以传递数据给子组件

class Child extends React.Component {
  render() {
    return (
        <div>{this.props.name}</div>
    );
  }
}

class Father extends React.Component {
  render() {
    return (
      <Child name={"Mr yang"} />
    );
  }
}
ReactDOM.render(<Father />, document.getElementById('root'));

但是这样如果想在父组件修改name的属相值,子组件也同步修改。这块就需要用到state了。
react state props可以去查资料看看两者的区别。

class Child extends React.Component {
  constructor(props) {
    super(props);
    //子组件通过父组件传递的值初始化state
    this.state={name : this.props.name}
  }
  //查阅上一篇文章了解一下
  componentWillReceiveProps(nextProps){
    this.setState({name : nextProps.name})
  }
  render() {
    return (
        <div>{this.state.name}</div>
    );
  }
}

class Father extends React.Component {
  //设置state
  state = {name:"Mr Yang"}
  //必须通过setState来修改
  click(){
    this.setState({name:"Mr Muyi" });
  }
  render() {
    return (
      <div>
        <Child name={this.state.name} />
        <button onClick={this.click.bind(this)}>click</button>
      </div>
    );
  }
}
ReactDOM.render(<Father />, document.getElementById('root'));

父组件通过refs调用子组件实例。具体查阅官网资料。
https://reactjs.org/docs/refs-and-the-dom.html

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);
    this.focusTextInput = this.focusTextInput.bind(this);
  }

  focusTextInput() {
    console.log('hello')
  }
  render() {
    return (
      <div>
        <input type="text"/>
      </div>
    );
  }
}

class AutoFocusTextInput extends React.Component {
  constructor(props) {
    super(props);
    //初始化ref
    this.textInput = React.createRef();
  }
  //完成加载的时候调用子组件方法
  componentDidMount() {
    this.textInput.current.focusTextInput();
  }

  render() {
    return (
      <CustomTextInput ref={this.textInput} />
    );
  }
}

ReactDOM.render(<AutoFocusTextInput />, document.getElementById('root'));

子组件通过调用父组件注册函数,调用父组件方法

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);
    this.getFather = this.getFather.bind(this);
  }

  getFather() {
    console.log('hello')
    //调用父组件方法
    this.props.MakeMoney('who are you');
  }
  componentDidMount(){
    this.getFather();
  }
  render() {
    return (
      <div>
        <input type="text"/>
      </div>
    );
  }
}

class AutoFocusTextInput extends React.Component {
  constructor(props) {
    super(props);
  }
 //供子组件调用
  MakeMoney(msg){ 
    console.log(msg);
  }

  render() {
    return (
      <CustomTextInput  MakeMoney={this.MakeMoney} />
    );
  }
}

ReactDOM.render(<AutoFocusTextInput />, document.getElementById('root'));

相关文章

  • react学2-父子组件通信

    react单项数据流动 父组件可以传递数据给子组件 但是这样如果想在父组件修改name的属相值,子组件也同步修改。...

  • React父子组件间通信的实现方式

    React学习笔记之父子组件间通信的实现:今天弄清楚了父子组件间通信是怎么实现的。父组件向子组件通信是通过向子组件...

  • 「React Native」Event Bus,消息总线

    (一)父子间组件通信:   一般使用props,回调函数进行通信。(二)跨组件之间通信:  (1)React Na...

  • react 组件通信

    概述 react中的组件通信问题,根据层级关系总共有四种类型的组件通信:父子组件、爷孙组件、兄弟组件和任意组件。前...

  • React入门基础知识总结

    1.React组件 function组件, class组件,来自ES6的class语法, 2. 父子组件通信 父组...

  • vue中的组件通信

    一、组件通信(组件传值) 1.1父子组件通信 1.2子父组件通信 1.3非父子组件通信(兄弟组件通信)

  • React02-组件通信

    React父子组件之间如何通信父组件传一个函数给子组件,子组件在适当的时候调用这个函数React爷孙组件之间如何通...

  • React 父子组件通信

    通讯是单向的,数据必须是由一方传到另一方。 1.父组件与子组件间的通信。 在 React 中,父组件可以向子组件通...

  • react父子组件通信

    父组件通过props 给子组件传递数据,子组件则是通过调用父组件传给它的函数给父组件传递数据。

  • react父子组件通信

    父组件向子组件通信 回调函数 直接把函数传到组件里面,然后组件里面调用this.props.goDetail函数来...

网友评论

      本文标题:react学2-父子组件通信

      本文链接:https://www.haomeiwen.com/subject/ewvteftx.html