简体   繁体   中英

How to pass props value to form field in react native?

i have this form code:

<Form>
    <Item floatingLabel last>
      <Label>Email</Label>
      <Input onChangeText={ (text)=> this.setState({email: text}) }
       />
    </Item>
    <Item floatingLabel last>
      <Label>Mobile</Label>
      <Input onChangeText={ (text)=> this.setState({reg_mob_no: text}) }
         />
    </Item>
    <Item disabled floatingLabel last>
      <Label>Package : {this.props.package_name}</Label>
      <Input disabled onChange={(text) => this.setState({package_select:this.props.package_id}) }
   />
    </Item>
    <Item floatingLabel last>
      <Label>Password</Label>
      <Input onChangeText={ (text)=> this.setState({password: text}) }
         secureTextEntry={true}/>
    </Item>
    <View padder>
        <Button block style={{ backgroundColor:"#FF69B4" }} onPress={this.onRegisterPressed.bind(this)} >
            <Text>Submit</Text>
          </Button>
    </View>  
  </Form>

i want to pass props value to package_select form field. this.props.package_id is the prop and prop value is a integer.

how will i pass prop value to form field ?

value={this.state.package_select}

When you pass package_select via props this line is wrong. It must be:

value={this.props.package_select}

Also keep in mind that when you pass props from the outside to an input field you have a controlled component. You can not change the internal state of that component on value change. So you register a callback on this component via props that is called, when the select input changes.

Read and understand this: https://reactjs.org/docs/uncontrolled-components.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM