如何将反对原生的道具值传递给表单字段?

时间:2017-10-14 07:59:32

标签: javascript reactjs react-native textinput

我有这个表单代码:

<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>

我想将props值传递给package_select表单字段。this.props.package_id是prop和prop值是一个整数。

我如何将prop值传递给表单字段?

1 个答案:

答案 0 :(得分:-1)

value={this.state.package_select}

当您通过道具传递package_select时,此行是错误的。必须是:

value={this.props.package_select}

另请注意,当您将道具从外部传递到输入字段时,您将拥有受控制的组件。您无法在值更改时更改该组件的内部状态。因此,当选择输入发生变化时,通过调用的props注册此组件的回调。

阅读并理解:https://reactjs.org/docs/uncontrolled-components.html