【问题标题】:React-select default value is not being translated with React-i18nextReact-select 默认值没有被 React-i18next 翻译
【发布时间】:2021-05-19 09:16:16
【问题描述】:

我已经如下设置了 Select 组件

                <Select
                    styles={customStyles}
                    defaultValue={{ label: t('Text to be translated'), value: 'all' }}
                    components={{
                       IndicatorSeparator: () => null,
                        }}
                    isSearchable={isSearchable}
                    options={[
                       { value: 'val1', label: t('text 1') },
                       { value: 'val2', label: t('text 2') },
                       { value: 'val3', label: t('text 3') },
                   ]}
                   onChange={handleSelectChange}
                   isDisabled={disabled}
                />

我的defaultValue 仅在首次加载组件时翻译。更改语言不会以任何方式产生影响。文本以初始加载时选择的语言显示。但有趣的事实 - 选项翻译完全没有问题

【问题讨论】:

    标签: reactjs internationalization react-select


    【解决方案1】:

    您应该将键作为参数传递给您的 Select 元素。确保它具有独特的价值,我认为 label 来自 defaultValue 对它有好处。引用以下官方文档

    键帮助 React 识别哪些项目已更改、添加或删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 2022-06-12
      • 2019-01-25
      • 2021-09-18
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多