您好,如何发送params导航返回?
const Onsubmit = (data, details = null) => {
console.log(details.formatted_address);
route.params.onPlaceChosen(
route.params.id,
details.formatted_address,
details.geometry
);
navigation.goBack();
};
在这里,我想将details.formated_address中的值传递到页面B。如何?
如果您正在从屏幕A导航到屏幕B,并且当您想返回屏幕A并在屏幕A中再次运行回调时,您需要执行以下操作:
在您的屏幕A(来源(
...
const onPlaceChosen = (params) => {
// here is your callback function
}
...
navigation.navigate('ScreenB', { onPlaceChosen })
...
在您的屏幕B(目的地(中
..
const Onsubmit = (data, details = null) => {
navigation.state.params.onPlaceChosen(
route.params.id,
details.formatted_address,
details.geometry
);
navigation.goBack();
};
...
我是这样做的:
onPress={() => {
// Pass and merge params back to home screen
navigation.navigate({
name: 'Home',
params: { post: postText },
merge: true,
});
}}
它摘自:https://reactnavigation.org/docs/params#passing-参数对预览屏幕
我基于https://reactnavigation.org/docs/5.x/hello-react-navigation/#passing-附加道具
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>
在我的案例中,使用道具传递的导航和路线,解决方案是:
route.params.onFilter({
route.params.id,
details.formatted_address,
details.geometry
});
navigation.goBack();