如何使用react native设置defaultValue?

问题描述 投票:0回答:1

我正在使用本机基础的输入,我无法为输入字段设置默认值。我正在IOS设备上进行测试。以下是我输入的代码:

  <Item floatingLabel>
              <Label>Email</Label>
              <Input
                onChangeText={email => this.setState({ email })}
                defaultValue={this.props.navigation.state.params.email}
                getRef={c => (this._inputEmail = c)}
                onSubmitEditing={event => {
                  this._inputPhone._root.focus();
                }}
                returnKeyType="next"
                keyboardType="email-address"
                autoCapitalize="none"
                autoCorrect={false}
              />
            </Item>

我以前在TextInput中使用它并且它工作正常。我不明白为什么它不适用于输入。任何帮助或建议表示赞赏。谢谢:)

reactjs react-native input textinput native-base
1个回答
0
投票

据我所知,NativeBase在Input上没有defaultValue prop,但你可以使用onChangeText和值prop的组合,如下所示:

<Input
onChangeText={email => this.setState({ email })}
value={this.state.email}
getRef={c => (this._inputEmail = c)}
onSubmitEditing={event => {
    this._inputPhone._root.focus();
}}
returnKeyType="next"
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
/>

在构造函数中设置默认值或在componentDidMount()中使用setState

© www.soinside.com 2019 - 2024. All rights reserved.