我有一个从react-native-picker-select挑选的人。我也有按钮,如果我按下它,我想把道具传递给挑选者,并把它作为标签显示。
我尝试从按钮中设置一个callback函数,将道具传递给选择器,并将标签设置为道具。我可以看到道具正在传递,但拾取器没有更新。
Score.js
<View style={styles.button}>
<TouchableOpacity
style={styles.scoreButton}
onPress={() => this.props.pressMe()}>
<Text style={{ color: 'white' }}>Press</Text>
</TouchableOpacity>
</View>Main.js
pressMe = () => {
if (this.state.level== 1) {
this.setState({ scoreInput: 100 });
}
};
<Deadlift
scoreInput={this.state.scoreInput}/>
<View style={styles.scoreContainer}>
<Score
pressMe={this.pressMe} />
</View>Deadlift.js
var picker = (<Picker
{...this.props}
style={pickerSelectStyles}
placeholder={{
label: 'Weight',
value: null,
}}
items={this.state.items}
onValueChange={value => {
onDLHandler(value, this.getScore(value));
}}
value={this.props.scoreInput} <- outside of picker shows this is
logged as 100 but value is not being updated on picker
/>)
<View>{picker}</View>发布于 2019-10-26 07:22:44
根据“价值”道具的文档
将尝试通过检查每个项的value属性从items数组中定位匹配项。如果找到,它将更新组件以显示所选的该项。如果找不到该值,它将默认为第一项。
选择器找不到项数组中的对象"{ scoreInput: 100 }“,因此选择器不会更新。如果要更新选择器,还必须添加项数组。
发布于 2019-10-26 01:38:17
如doc所述,项应该是带有标签和值键的对象数组,而不仅仅是值数组。
https://github.com/lawnstarter/react-native-picker-select/blob/master/README.md#props
https://stackoverflow.com/questions/58563018
复制相似问题