尝试将值作为导航参数传递,但无法在另一个组件中获取该参数
2020-05-28
47
我正在尝试在我的应用程序上实现 i18n,我设置了一个名为
SettingsScreen.js
的文件,此文件有很多按钮可以更改语言(参见下面的示例),还有另一个名为
HomeScreen.js
的文件,此文件应该通过
SettingsScreen.js
传递参数并更改应用程序的语言,但我收到此错误:
所以我要发布其余的代码以下:
HomeScreen.js:
export default function HomeScreen({ route, navigation }) {
let { lang } = route.params;
i18n.locale = lang;
<Rest of the content/>
}
SettingsScreen.js:
export default function SettingsScreen({ navigation }) {
let setPortuguese = () => {
navigation.navigate("Dashboard", {
screen: "HomeScreen",
params: { lang: "pt" }
})
<Rest of the content />
}
}
app.js:
export default function App() {
return (
<NavigationContainer>
<Drawer.Navigator>
<Drawer.Screen name="Dashboard" component={Dashboard} />
<Drawer.Screen name="Settings" component={SettingsScreen} />
</Drawer.Navigator>
</NavigationContainer>
);
}
1个回答
问题在于调用
navigation.navigate
传递了错误的参数。
尝试此代码:
SettingsScreen.js:
export default function SettingsScreen({ navigation }) {
let setPortuguese = () => {
navigation.navigate("HomeScreen", {
lang: "pt"
})
<Rest of the content />
}
}
Maycon Mesquita
2020-05-28