Rocky
Rocky

Reputation: 442

How to Pass Data on previous Screen after clicking on Back Button in React native?

Currently I'm working in react native, I have two pages A and B, I'm already on A Screen after clicking on continue button I moved on B Screen , there is an back button. now my question is :=> if I click on back Button then I want to pass my Name on previous Screen from state:

Have tried this:

1.this.props.navigation.goBack({'Name':'a'})

2.GoBack=async(data)=>{
   if (data !== null) {
      this.props.navigation.navigate('A', {
      onGoBack: () => this.refresh(),
     'Name':'a',
    });
   }}

3.this.props.navigation.navigate({'Name':'a'})

In This method I'm getting my Name in previous Screen but state is not getting update after refreshing it's showing name.

Upvotes: 0

Views: 2207

Answers (3)

Rocky
Rocky

Reputation: 442

finally it's working : Thanks everyone :)

 this.props.navigation.navigate("A", {
     'Name':'a',
     onGoBack: () => {
      this.refresh()
     }
  });

Upvotes: 0

Kishan Bharda
Kishan Bharda

Reputation: 5700

Try below code :

this.props.navigation.navigate("A", {
    'Name':'a',
    onGoBack: () => {
        this.refresh()
    }
});

And your screen 'A' :

this.props.navigation.state.params.onGoBack();
this.props.navigation.goBack();

Upvotes: 1

James Liu
James Liu

Reputation: 279

  1. Redux is a good tool to manage state.
  2. Why don't you try
this.props.navigation.addListener(
            'willFocus',
            payload => {
                this.setState({Name:payload.Name});
            }
        )

Upvotes: 0

Related Questions