【发布时间】:2019-10-25 17:18:18
【问题描述】:
我有一个来自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>
【问题讨论】:
-
你的 scoreOutput 组件在哪里
-
刚刚更新了
-
在 main.js 中将
pressMe={this.pressMe}更改为pressMe={(value) => this.pressMe(value)}并删除 {...this.props} 并在 scoreOutput.js 中添加onValueChange={(value) => this.props. pressMe(value)} -
那行不通。另外,我没有将 pressMe 直接传递给 scoreOutput。我将它传递给 main.js,然后将 scoreInput 道具传递给 scoreOutput。
-
@fosho 您的 scoreOutput 组件在哪里主要或某处使用?我只看到 scoreInput
标签: reactjs react-native