作者:多多智能锁 | 来源:互联网 | 2023-02-01 19:37
我不确定何时在组件中使用onChange
vs.我知道在回调中接受更改的文本作为arg,但这就是你要使用的原因,因为你可以在回调中更新状态?onChangeText
TextInput
onChangeText
onChangeText
1> magneticz..:
onChangeText
基本上是一个简化版本onChange
,因此您可以轻松使用它,而无需经历{ nativeEvent: { eventCount, target, text} }
改变价值的麻烦.
那么,你何时应该使用eventCount
何时target
?
如果你有简单的形式,几乎没有文字输入或简单的逻辑,你可以直接使用eventCount
const MyTextInput = ({ value, name, type, onChange }) => {
return (
onChange({ name, type, text })}
/>
);
};
如果您有更复杂的表单和/或您在处理数据时有更多逻辑(比如处理文本与数字不同),那么您最好使用TextInput
,因为它为您提供了更大的灵活性.例如:
handleChange(event) {
const {name, type, text} = event;
let processedData = text;
if(type==='text') {
processedData = value.toUpperCase();
} else if (type==='number') {
processedData = value * 2;
}
this.setState({[name]: processedData})
}
据我所知,在React Native的TextInput中没有名为name的prop.对于反应,这没关系.https://facebook.github.io/react-native/docs/textinput.html
`nativeEvent`没有这些属性。{native事件:{eventCount,目标,文本}}