【发布时间】:2019-11-03 16:36:25
【问题描述】:
我在让我的一个字段预先填充信息并进行编辑时遇到问题。我尝试在代码中移动它使用数据设置字段并且它是空白且可编辑或显示预填充的数据,但 UI 阻止我编辑它。
我遇到的问题是 bar 字段。将其放入构造函数中会使用 info 预先填充该字段,但 UI 阻止我对其进行编辑。为什么?应该在哪里设置此字段或如何修复它?在导航到此页面之前,我是否需要调用该对象的填充位置,以便在构造函数初始化期间填充它或..?
这是类组件sn-p:
export class FooBarBazComponent extends Component{
constructor(props){
super(props);
this.state = {
foo: "",
bar: ""
};
const fooDetails = this.props.navigation.state.params.fooDetails;
this.state.foo = fooDetails.foo;
}
render(){
const disabled = this.state.foo.length !== 5 || this.state.bar.length < 5;
//I didn't put this code in the constructor because this object is undefined in the constructor
if(this.props.objResponse) {
this.state.bar = this.props.objResponse.bar;
}
return(
<View style={Styles.inputRow}>
<View style={Styles.inlineInput}>
<FormLabel labelStyle={Styles.label}>FOO</FormLabel>
<TextInputMask
onChangeText={foo => this.setState({ foo })}
value={this.state.foo}
/>
</View>
<View style={Styles.inlineInput}>
<FormLabel labelStyle={Styles.label}>BAR</FormLabel>
<TextInputMask
onChangeText={bar => this.setState({ bar })}
value={this.state.bar}
/>
</View>
</View>
);
}
}
【问题讨论】:
标签: javascript reactjs react-native setstate