【问题标题】:react-native: <Picker> loses selectedValue when changing the <Picker.Item> labelreact-native: <Picker> 在更改 <Picker.Item> 标签时丢失 selectedValue
【发布时间】:2016-11-03 18:44:21
【问题描述】:

我想更改我的&lt;Picker.Item&gt;s 的标签 &lt;Picker&gt;,例如当语言发生变化时,由另一个按钮触发 (react-redux)。

keyvalue 应该保持不变,只是 label 发生变化。

不幸的是,结果是重新渲染(?),selectedValue 更改为第一个 &lt;Picker.Item&gt;s(不是默认值!)。

<Picker 
    selectedValue={this.props.myValue}
    onValueChange={(newValue) => this.props.setMyValue(newValue);}}>

    {this.props.myOptions.map((s, i) => {
        let l = modify(s);
        return <Picker.Item key={i} value={s} label={l}/>      
    })}
</Picker>

只要label 发生更改,就会出现问题,例如通过在每次更改时添加当前日期:

export default function modify(string) {
    // return string; // works
    return string + '_i_change_' + new Date(); // doesn't work
}

我感谢每一个建议。 提前致谢!

【问题讨论】:

    标签: javascript android reactjs react-native redux


    【解决方案1】:

    遇到了同样的问题。您可能需要将 selectedValue 设为字符串。例如:

    selectedValue={this.props.myValue.toString()}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多