最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to pass data back to previous screen in react native navigation v5? - Stack Overflow

matteradmin3PV0评论

I just updated to react native navigation version 5. Now I am trying to send data back to previous screen on goBack() call.

I push next view with

const onSelectCountry = item => {
    console.log(item);
};

navigation.navigate('SelectionScreen', {
        onSelect: onSelectCountry});

And making move back after selecting item from FlatList with call:

function onSelectedItem(item) {
    route.params.onSelect(item);
    navigation.goBack();
}

But by sending function over with params I get a warning: Non-serializable valuse were found in the navigation state...

Can someone please tell me correct way to do this.

I just updated to react native navigation version 5. Now I am trying to send data back to previous screen on goBack() call.

I push next view with

const onSelectCountry = item => {
    console.log(item);
};

navigation.navigate('SelectionScreen', {
        onSelect: onSelectCountry});

And making move back after selecting item from FlatList with call:

function onSelectedItem(item) {
    route.params.onSelect(item);
    navigation.goBack();
}

But by sending function over with params I get a warning: Non-serializable valuse were found in the navigation state...

Can someone please tell me correct way to do this.

Share edited Mar 3, 2020 at 8:35 schmru asked Mar 2, 2020 at 15:04 schmruschmru 6193 gold badges11 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

heres is an implementaion

scereen A

const Screen1 = ({navigation, route}) => {
  const [item, setItem] = useState(null);

  useEffect(() => {
    navigation.addListener('focus', () => {
      console.log(route.params)
    })
  }, [])  

  const onPress = () => {
    navigation.navigate('Screen2', {onReturn: (item) => {
      setItem(item)
    }})
  }
  return (
    // Components
  )
}

Screen2:

const Screen2 = ({navigation, route}) => {

  useEffect(() => {
    navigation.addListener('focus', () => {
      console.log(route.params)
    })
  }, [])  

  // back Press
  const onPress = () => {
    route.params.onReturn(item);
    navigation.goBack()
  }

  return (
    // Components
  )
}

navigation send data to screens.

onPress={() => {
          // Pass params back to home screen
          navigation.navigate('Home', { post: postText });

follow official documentation React Native

I visited this post because I wanted to use the same mon ponent in 2 stacks. How to know how to go back and pass params?

I solved it by passing first a parameter to go there, which will identify where the ponent is accessed from. Then, instead of using goBack(), which doesn't accept parameters, I navigate to the previous route and pass the parameter.

//in Stack1
navigation.navigate('monComponent', isStack1: true)
//in Stack2
navigation.navigate('monComponent', isStack1: false)
//in CommonComponent, instead of goback(), use navivation.navigate

function CommonComponent({ navigation, route }) {
  const {isStack1} = route.params
  const customRoute = isStack1 ? 'routeNameInStack1' : 'routeNameInStack2'
  return (
    <Button title="Go back" onPress={() => navigation.navigate('customRoute', {myParams: 'blabla...') />
  )
}
Post a comment

comment list (0)

  1. No comments so far