简体   繁体   English

在React Native中使用goBack()时,如何检测语言是否已更改?

[英]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.

相关问题 React Native,reactnavigation,如何使用 Drawer 返回()到上一个屏幕,而不是第一个屏幕? - React Native, reactnavigation, how do I goBack() to the previous screen, not the first screen, using a Drawer? 您如何检测屏幕的右半部分是否已被本机点击? - How do you detect if the right half of the screen has been tapped in react-native? 使用 i18next 从应用程序更改时,如何使反应本机应用程序保存语言? - How to make react native app save the language when changed from the app with i18next? 使用 Hooks 使用 React Native 返回时刷新屏幕 A - Refresh Screen A when goBack with React Native using Hooks React Native应用启动后,如何提取Expo QR代码? - How do I pull up the Expo QR code after React Native app has been started? React native - 发送消息后如何刷新屏幕? - React native - how to refresh screen when message has been sent? 我如何确定是否可以使用 react-router goBack() 或 goForward()? - How do I find out if it's possible to goBack() or goForward() using react-router? 在本机反应中使用 goBack() 需要帮助 - Need help using goBack() in react native React Native:使用pop()或goBack()时出现性能问题,导致无限循环。 但是使用`reset`就可以了 - React Native: Performance issue when using `pop()` or `goBack()`, causing an infinite loop. But using `reset` is fine 在 React Native 上获得此 position 后,如何检测用户何时获得视图并执行某些操作? - How can I detect when the user gets a View and do something after getting this position on React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM