react-native - 在 i18next 中遍历数组

标签 react-native i18next react-i18next

有一种方法可以迭代和呈现字符串/对象数组中的每一项吗?

i18n
    .use(languageDetector)
    .use(reactI18nextModule)
    .init({
        fallbackLng: 'en',
        resources: {
            en: {
                home: {
                    data:[
                        {id:1,val:"Item1"},
                        {id:2,val:"Item2"},
                        {id:3,val:"Item3"},
                        {id:4,val:"Item4"}]
                },
            }
        }
});

在组件中:

<FlatList
        data={t('home:data')} //--cannot do this
        keyExtractor={(item, index) => index}
        renderItem={({item}) => <Item>{item.val}</Item>}
/>

最佳答案

我找到了一个方法

<FlatList
    data={t('home:data', { returnObjects: true })}
    keyExtractor={(item, index) => index}
    renderItem={ ({item}) => <Text> {item.val}</Text>}
/>

FlatList 将每个元素包装在一个 item 对象中 (https://facebook.github.io/react-native/docs/flatlist.html)。

关于react-native - 在 i18next 中遍历数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50238951/

相关文章:

react-i18next - 如何修复 '' 类型错误 : createContext is not a function"

react-native - 如何在 Windows 10 上使用 React-Native 构建 iOS 应用程序

javascript - 没有路线匹配位置 "/rewards-store"

ios - 通过 React Native Firebase 存在 AdMob 时出现 “Linker command” 错误

javascript - undefined 不是一个函数(评估'_this.props.navigation.navigate')

javascript - 从后端加载数据时,I18Next 不起作用

jquery - 替换 i18next 中的变量

reactjs - 如何更改默认值 react-i18next(在 localstorage 中)

typescript - 带有 i18next-node-fs-backend 的 i18next 显示键而不是字符串

reactjs - React Maximum update depth exceeded 错误仅在测试中由useEffect引起