boy*_*y_v 16 javascript reactjs react-router react-native
如何用 React Native 的 React Navigation 替换屏幕
现在我是新手,我无法理解 getStateForAction 现在我在屏幕 1 上有参数,并通过传递参数用户名导航到屏幕 2 基本导航它的嵌套屏幕 1 > 2 在堆栈上
但我需要在屏幕 2 处于活动状态后将屏幕 1 替换为屏幕 2(像路由器通量上的 ActionConst.REPLACE 一样替换)并以新方式发送参数
有人可以指导我吗谢谢。
屏幕1
onPress = () => {
this.props.navigation.navigate('Sc2', {username: this.state.username});
}
Run Code Online (Sandbox Code Playgroud)
屏幕2
componentWillMount() {
const {state} = this.props.navigation;
console.log(state.params.username);
}
Run Code Online (Sandbox Code Playgroud)
路由器
export const LoginNavStack = StackNavigator({
Sc1: {
screen: Sc1
},
Sc2: {
screen: Sc2,
},
});
Run Code Online (Sandbox Code Playgroud)
Aaj*_*ngh 18
如果有人仍在弄清楚如何替换 React Native 中的屏幕并正在使用react-navigation,这里是:
import { StackActions } from '@react-navigation/native';
navigation.dispatch(
StackActions.replace('Profile', {
user: 'jane',
})
);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅:https://reactnavigation.org/docs/stack-actions/#replace
使用这个代替 navigation.replace
navigation.reset({
index: 0,
routes: [{ name: 'Profile' }],
});
Run Code Online (Sandbox Code Playgroud)
您可以使用navigation.replace
使用
this.props.navigation.replace('Sc2');
Run Code Online (Sandbox Code Playgroud)
代替
this.props.navigation.navigate('Sc2', {username: this.state.username});
Run Code Online (Sandbox Code Playgroud)
您可以从这里找到更多信息https://reactnavigation.org/docs/en/navigation-key.html
| 归档时间: |
|
| 查看次数: |
40119 次 |
| 最近记录: |