How to pass data between child and parent in react-native?

后端 未结 6 1850
一生所求
一生所求 2020-12-30 07:49
module.exports= class APP extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      key1:\'key1\',
      key2:\'key2\'
    };
  rend         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 08:02

    You need to pass from parent to child callback function, and then call it in the child.

    For example:

    class Parent extends React.Component {
      constructor(props){
        super(props);
        this.state = {
          show: false
        };
      }
      updateState = () => {
          this.setState({
              show: !this.state.show
          });
      }
      render() {
        return (
            
        );
      }
    }
    
    class Child extends React.Component {
      handleClick = () => {
          this.props.updateState();
      }
      render() {
        return (
            
        );
      }
    }
    

提交回复
热议问题