bzl*_*ght 0 javascript textinput reactjs react-native
期望状态
我在 react native 中有两个文本输入字段,每个字段的最终输入都需要附加到数组中。因此我使用 onSubmitEditing 因为否则如果我使用 onChangeText 用户输入的每个字符都会附加到数组中。
错误
onSubmitEditing 调用父组件中的函数并给出以下错误
“ExceptionsManager.js:84 警告:出于性能原因重用此合成事件。如果您看到这一点,则表示您正在访问已nativeEvent发布/无效的合成事件的属性。这被设置为 null。如果您必须保留原始周围的合成事件,使用 event.persist()。”
我尝试将函数移动到同一个文件,这并不理想,但我仍然返回这个数组而不是文本输入。数组中似乎没有任何有用的东西。
“[合成事件]”
代码
子组件
<TextInput
style={styles.signupInput}
onSubmitEditing={(val) => this.props.saveFriends(val)}
autoCorrect={false}
autoFocus={true}
placeholder={'friend one'}
placeholderTextColor={'white'}
/>
<TextInput
style={styles.signupInput}
onSubmitEditing={(val) => this.props.saveFriends(val)}
autoCorrect={false}
autoFocus={true}
placeholder={'friend two'}
placeholderTextColor={'white'}
/>
Run Code Online (Sandbox Code Playgroud)
父组件
saveFriends = (val) => {
this.setState({
friends: [
...this.state.friends,
val
]
})
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
因此,当您使用该onSubmitEditing属性时,传递给回调的参数是一个事件对象。为了访问您的文本并将其保存到数组中,您有两种解决方案:
第一种解决方案,访问事件的正确属性
<TextInput
style={styles.signupInput}
onSubmitEditing={(event) => this.props.saveFriends(event.nativeEvent.text)}
autoCorrect={false}
autoFocus={true}
placeholder={'friend one'}
placeholderTextColor={'white'}
/>Run Code Online (Sandbox Code Playgroud)
其次,使用Button, 并onChangeText在组件状态中保存输入的值:
<TextInput
style={styles.signupInput}
onChangeText={(val) => this.setState({val})}
autoCorrect={false}
autoFocus={true}
placeholder={'friend one'}
placeholderTextColor={'white'}
/>
<Button
onPress={() => this.props.saveFriends(this.state.val)}
/>Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
1571 次 |
| 最近记录: |