我正在使用react-i18next
和react-navigation
。
目前,当有需要时,我会单独用withNamespaces
包装所有组件。问题是我无法保持title
的static navigationOptions
最新。它不会更新,无论我如何分配它:作为函数或属性对象。 navigation.setParams
也没有更新它。
我尝试在Navigators上使用withNamespaces
来使用screenProps
,因为它完成了here,但在这种情况下我的调度NavigationActions
没有效果。导航就不会发生。
我假设i18next
HOC以某种方式阻止其子女接收params更新事件。我是否需要以其他方式初始化i18next
以解决此问题?或者有没有办法迫使title
中的navigationOptions
更新?
好的,我想出了一个解决这个问题的简单方法。我创建了一个只返回所需字符串的组件,然后用withNamespaces
将其包装起来并将其放入title
的navigationProperties
道具中。工作良好。
这是一个示例代码。
标题组件
import React from 'react'
import PropTypes from 'prop-types'
import { withNamespaces } from 'react-i18next'
import { Text } from 'react-native'
const ScreenTitle = ({ path, t }) => <Text>{t(path)}</Text>
ScreenTitle.propTypes = {
path: PropTypes.string.isRequired,
t: PropTypes.func.isRequired,
}
export default withNamespaces()(ScreenTitle)
用法
static navigationOptions = () => {
return {
title: <ScreenTitle path="privacyPolicy:title" />,
}
}