14

I've got screen 1 from which I navigate to screen 2 using:

navigation.navigate('Screen2')

From this screen, I would like to go to the previous one, which simple:

navigation.goBack()

However I'm wondering how can I pass some data back to Screen1? Something like wouldn't work navigation.goBack({ myData: 'something' }) so I'm wondering what is the recommended way to do this in general?

6 Answers 6

33

You can solve it with 2 ways :

1 : Using navigation method

Pass a method when you are calling that screen through navigation :

this.props.navigation.navigate('Screen2', {
  onGoBack: this.refresh,
});

refresh=(data)=> {

}

and when you press back, pass data like

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

2 : Using redux store

If you are using redux in your application, just update redux store whenever user presses back button, and get store value in previous screen.

Sign up to request clarification or add additional context in comments.

3 Comments

i have a similar question, i can navigate from Screen1 to Screen2 passing all the params as well, in Screen2 i updated one of the params passed and now i want to update it in Screen1 as well but not navigate to Screen1. Is that possible?
This is not recommended since the library will warn you that passing in functions as parameters can be problematic since it can't be serialized
Please see the recommended approach in React Navigation docs: reactnavigation.org/docs/params/…
12

You can pass a callback (onSelect) like this:

SCREEN 1

import React from "react";
import { Button, Text, View } from "react-native";

class Screen1 extends React.Component {
  state = { selected: false };

  onSelect = data => {
    this.setState(data);
  };

  onPress = () => {
    this.props.navigate("Screen2", { onSelect: this.onSelect });
  };

  render() {
    return (
      <View>
        <Text>{this.state.selected ? "Selected" : "Not Selected"}</Text>
        <Button title="Next" onPress={this.onPress} />
      </View>
    );
  }
}

SCREEN 2

import React from "react";
import { Button } from "react-native";

class Screen2 extends React.Component {
  goBack() {
    const { navigation } = this.props;
    navigation.goBack();
    navigation.state.params.onSelect({ selected: true });
  }

  render() {
    return <Button title="back" onPress={this.goBack} />;
  }
}

Comments

0

if you're using v2 or newer, another possibility is using the navigate function, providing key / routeName of the route you're going back to and the params. docs: https://reactnavigation.org/docs/en/navigation-actions.html#navigate

Comments

0

Using the navigation method Pass a method when you are calling that screen through navigation :

this.props.navigation.navigate('Screen2', {
  onGoBack: this.refresh,
});

refresh=(data)=> {
   console.log(data)
}

and when you press back, pass data like

this.props.route.params.onGoBack('123');
this.props.navigation.goBack();

Comments

0
// PreviousScreen.js
const PreviousScreen = () => {
  const handleData = (data) => {
    // Handle the received data here
    console.log('Received data:', data);
  };

  // Navigate to the next screen and pass the callback function as a parameter
  const goToNextScreen = () => {
    navigation.navigate('NextScreen', { callback: handleData });
  };

  // ...
};/
// NextScreen.js
const NextScreen = ({ navigation, route }) => {
  // Retrieve the callback function from the navigation parameters
  const callback = route.params?.callback;

  // Call the callback function with the data you want to send back
  const sendDataToPreviousScreen = () => {
    const data = 'Hello from NextScreen!';
    callback && callback(data); // Make sure the callback exists before calling it
    navigation.goBack();
  };

  // ...
};

Comments

-2

yes goback work for going previous screen to ...and show our data call from api ...goBack()

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.