Sennen De Almeida
Sennen De Almeida

Reputation: 51

How to get state values of other component in React-Native?

showConfirmationMessage = () => {
    Alert.alert(
      'Confirmation Message',
      'Proceed?',
      [
        {text: 'BACK', onPress: () => this.setState({ loading: false })},
        {text: 'CONFIRM', onPress: () => this._getTaskData()},
      ],

      {cancelable: false},
    );
  }



_getTaskData = () => {
    console.log(this.component2.getValue());
}

This is how I called the function. When I called _getTaskData() directly, it works fine. But, when I call it like above (through the confirmation message), it gives the error.

Upvotes: 0

Views: 178

Answers (1)

SiSa
SiSa

Reputation: 2684

Write a function inside Component2 to get your input values:

getValue = () => {
    return this.state.inputValue;
}

and set a reference for your Component2 inside Component1

<Component2 ref={r => this.component2 = r} />

Now you can get your input value with your this.component2 reference

this.component2.getValue();

Upvotes: 1

Related Questions