Sunday, 10 November 2019

How can I make my input field prefilled with data and editable on page load?

I'm having issues getting one of my fields to pre-populate with info and be editable. I've tried moving around the code where it sets the field with the data and either it's blank and editable or shows the pre-populated data but the UI prevents me from editing it.

The issue I'm having is with the bar field. Putting it in the constructor pre-populates the field with info but the UI is preventing me from editing it. Why? Where should this field be set or how can I fix it? Do I need to call where this object gets populated before navigating to this page, so it gets populated during constructor initialization or..?

Here's the class component snippet:

export class FooBarBazComponent extends Component {

constructor(props)
{
    super(props);
    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>
    );
  }
}
}


from How can I make my input field prefilled with data and editable on page load?

No comments:

Post a Comment