[英]How do I detect if a language has been changed when using the goBack() in React Native?
I have 3 pages that will be interacting with each other. 我有3个页面,它们将彼此交互。 a Login page, a Settings page, and a HomeScreen page.
登录页面,设置页面和主屏幕页面。 The login page contains a toolbar which has a clickable back arrow image(uses goBack()) and a clickable settings image which redirects to a settings page where a language can be picked.
登录页面包含一个工具栏,该工具栏具有可单击的后退箭头图像(使用goBack())和可单击的设置图像,该图像重定向到可以选择语言的设置页面。
There is no problem detecting a language change on the settings page because state is updated upon a change in language. 在设置页面上检测语言更改没有问题,因为状态会随着语言更改而更新。 However, if the user taps the backarrow image, the login page does NOT detect a change in state.
但是,如果用户点击backarrow图像,则登录页面不会检测到状态更改。 How do I make sure that the login page can detect if the language has been changed(on the Settings page)?
如何确保登录页面可以检测到语言是否已更改(在“设置”页面上)?
I found on question that is similar to my problem here however, the answer provided uses navigate, whereas I'm using goBack. 我的问题是类似于我的问题找到这里然而,答案提供使用导航,而我使用GoBack的。 I know they're similar, but I'm unsure as to how/where I could pass a callback function on my settings page, and where to use refresh() on my Login page.
我知道它们是相似的,但是我不确定如何/在哪里可以在设置页面上传递回调函数,以及在哪里在我的登录页面上使用refresh()。
I use this method on my Login page 我在登录页面上使用此方法
componentWillMount() {
AsyncStorage.getItem('language', (err, result) => {
langCode = result;
this.setState({
currLang: result,
})
});
}
On my Settings page: 在我的设置页面上:
onLangChange(value, index){
Config.langCode = value;
this.setState({ currLang: Config.langCode });
AsyncStorage.setItem('language', value)
console.log( 'here is your new lang ' + Config.langCode);
}
and then 接着
render(){
const {navigate} = this.props.navigation;
const {goBack} = this.props.navigation
const langText = Config.langText[this.state.currLang]; // Object that has text in current language.
return(
<ScrollView style={styles.mainContainer}>
<View style={styles.headerContainer}>
<View style={styles.iconContainer}>
<TouchableOpacity onPress={ () => goBack('') } >
<Image source={Config.images.leftArrowIcon} style={styles.leftArrowIcon} />
</TouchableOpacity>
Use redux
for global states like language
in your example. 将
redux
用于示例中的language
等全局状态。 It's just much simpler. 简单得多。 If you have like 30 screens in your app, and every screen must display texts in correct language, this means that you have to send
language
back and forth between 30 screens, that's just horrible. 如果您的应用程序中有30个屏幕,并且每个屏幕都必须以正确的语言显示文本,这意味着您必须在30个屏幕之间来回发送
language
,这太可怕了。 redux
is a state container, it's strongly recommended that you put those global states (states that many screens share) in redux
and send them to each screen. redux
是一个状态容器,强烈建议您将这些全局状态(许多屏幕共享的状态)放入redux
,并将其发送到每个屏幕。 If you don't know how to use redux
yet, don't worry, it's not hard to understand, there're plenty good tutorials online. 如果您还不知道如何使用
redux
,请不要担心,这并不难理解,在线上有很多不错的教程。 Here is the LanguagePickerExample I wrote for this question using redux , simply 这是我使用redux为这个问题编写的LanguagePickerExample ,简单来说
npm install
or 要么
yarn
then 然后
react-native link
The result looks like this: 结果看起来像这样:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.