反应原生:<Picker>更改 <Picker.Item> 时丢失 selectedValue标签 [英] react-native: <Picker> loses selectedValue when changing the <Picker.Item> label
本文介绍了反应原生:<Picker>更改 <Picker.Item> 时丢失 selectedValue标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改我的
的
的标签,例如当语言改变时,由另一个按钮触发(react-redux
).
key
和 value
应该保持不变,只是 label
发生了变化.
key
and value
should stay the same, just the label
changes.
不幸的是,结果是重新渲染(?)并且 selectedValue
更改为第一个
s(不是默认值!).
Unfortunately the result is a re-rendering(?) and the selectedValue
changes to the first <Picker.Item>
s (not to the default!).
<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
发生变化,问题就会发生,例如通过在每次更改时添加当前日期:
The problem occurs as soon as the label
changes, e.g. by adding the current date on every change:
export default function modify(string) {
// return string; // works
return string + '_i_change_' + new Date(); // doesn't work
}
我感谢每一个建议.提前致谢!
I appreciate every advice. Thanks in advance!
推荐答案
遇到了同样的问题.您可能需要将 selectedValue 设为字符串.例如:
ran into same issue. You may need to make the selectedValue a string. eg:
selectedValue={this.props.myValue.toString()}
这篇关于反应原生:<Picker>更改 <Picker.Item> 时丢失 selectedValue标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文