简体   繁体   English

在反应导航中使用 navigation.goBack 发送参数

[英]Sending params with navigation.goBack in react navigation

hellow, how to send params navigation go back?你好,如何发送参数导航 go 回来?

const Onsubmit = (data, details = null) => {
    console.log(details.formatted_address);
    route.params.onPlaceChosen(
      route.params.id,
      details.formatted_address,
      details.geometry
    );
    navigation.goBack();
  };

here I want to pass the value from details.formatted_address to page B. How to?在这里,我想将 details.formatted_address 中的值传递给页面 B。如何?

If you are navigating from Screen A to Screen B, and when you want to go back to Screen A with running a callback in again Screen A, below is what you need to do:如果您从屏幕 A 导航到屏幕 B,并且当您想返回屏幕 A 并再次在屏幕 A 中运行回调时,您需要执行以下操作:

In your Screen A (Source)在您的屏幕 A(来源)中

...
const onPlaceChosen = (params) => {
    // here is your callback function
}

...
navigation.navigate('ScreenB', { onPlaceChosen })
...

In your Screen B (Destination)在您的屏幕 B(目的地)中

..
const Onsubmit = (data, details = null) => {

    navigation.state.params.onPlaceChosen(
      route.params.id,
      details.formatted_address,
      details.geometry
    );
    navigation.goBack();
  };
...

In my case, using navigation and route passed by props, the solution was:在我的情况下,使用通过道具传递的导航和路线,解决方案是:

route.params.onFilter({
    route.params.id,
    details.formatted_address,
    details.geometry
});
navigation.goBack();

I did something like this based on https://reactnavigation.org/docs/5.x/hello-react-navigation/#passing-additional-props我根据https://reactnavigation.org/docs/5.x/hello-react-navigation/#passing-additional-props做了类似的事情

const [params, setParams] = useState({});   

<Stack.Navigator>
  <Stack.Screen name="One">
  {props => <FirstScreen {...props} paramsFromTwo={params} />}
  </Stack.Screen>  

  <Stack.Screen name="Two">
  {props => <SecondScreen {...props} onGoBack={(paramsFromSecond} => setParams(paramsFromSecond)} />}
  </Stack.Screen>  
</Stack.Navigator>

I did this way:我是这样做的:

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

It was extracted from: https://reactnavigation.org/docs/params#passing-params-to-a-previous-screen它摘自: https://reactnavigation.org/docs/params#passing-params-to-a-previous-screen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM