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

后端 未结 6 1824
一生所求
一生所求 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 07:53

    Using Props like this:

    Parent:

    <Parent>
      <Child onChange={this.change} />
    </Parent>
    

    Child:

    <button onclick={this.props.onChange('It Changed')} />
    

    With this you can just do whatever you want in your parent.

    0 讨论(0)
  • 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 (
            <Child updateState={this.updateState} />
        );
      }
    }
    
    class Child extends React.Component {
      handleClick = () => {
          this.props.updateState();
      }
      render() {
        return (
            <button onClick={this.handleClick}>Test</button>
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-30 08:07
    class Parent extends Component{
       constructor(props){
       super(props);
        this.state={value:''};
        this.getData=this.getData.bind(this);
      }
    
      getData(val){
       console.log(val);
      this.setState({
        value:val
      });
      }
        render(){
          const {value}=this.state
        return(
          <View>
          <Screen sendData={this.getData}/>
          <View>
            <Text>
        {this.state.value};
        </Text>
          </View>
          </View>
        )
        }
    }
    export default Parent;
    
    
    CHILD CLASS:
    
    class Child extends Component{
       componentWillMount(){
        this.props.sendData('data');
       }
        render(){ 
        return(
            <View>  
           </View>
        )
        }  
    }
    export default Child;
    
    0 讨论(0)
  • 2020-12-30 08:08

    Like you do with React.

    You have to exchange information through props, or use a library like Redux.

    0 讨论(0)
  • 2020-12-30 08:12

    This can be achieved by two ways:

    Parent Component:

    //Needs argument
    const addGoalHandler = goalTitle => {
            // code for updating state
    }
    
    <GoalInput onAddGoal={this.addGoalHandler} />
    

    Child Component:

    Way 1: Using Vanilla Javascript

    <Button 
        title="ADD"
        onPress={props.onAddGoal.bind(this, enteredGoal)}
    />
    

    Way 2: Using Arrow Function

    <Button 
        title="ADD"
        onPress={() => { props.onAddGoal(enteredGoal) } }
    />
    
    0 讨论(0)
  • 2020-12-30 08:19

    To call Parent's method from child, you can pass the reference like this.

    Parent Class

    <ChildClass
        onRef={ref => (this.parentReference = ref)}
        parentReference = {this.parentMethod.bind(this)}
    />
    
    parentMethod(data) {
    
    }
    

    Child Class

    let data = {
        id: 'xyz',
        name: 'zzz',
    };
    

    //This will call the method of parent

    this.props.parentReference(data);
    
    0 讨论(0)
提交回复
热议问题