React-Native onSubmitEditing - 对象作为 React Child 无效
React-Native onSubmitEditing - Objects are not valid as a React Child
我正在尝试在返回时获取文本输入,这样我就不必在每个键输入上连续 运行 和 API。当我在 onChangePress 上 运行 时代码有效,但是当我将其更改为 onSubmitEditing 时,我在代码下方收到错误。
export default function Search({navigation}) {
const [search, setSearch] = useState('123')
return (
<View style={styles.container}>
<SafeAreaView>
<View style={styles.header}>
<TouchableOpacity
style={styles.back}
onPress={() => navigation.goBack()}
>
<Ionicons name="arrow-back-circle-sharp" size={24} color="black" />
</TouchableOpacity>
<View style={styles.input}>
<Feather
name="search"
size={20}
color="black"
style={{textAlign: 'center'}}
/>
<TextInput
placeholder='Search for channels'
onSubmitEditing={(val) => setSearch(val)}
/>
</View>
</View>
</SafeAreaView>
<Text>{search}</Text>
<StatusBar style="auto" />
</View>
);
}
错误:
Error: Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, _dispatchListeners, _dispatchInstances, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, isDefaultPrevented, isPropagationStopped}). If you meant to render a collection of children, use an array instead.
如 react-native
文档所述 onSubmitEditing
returns 回调对象 ({ nativeEvent: { text, eventCount, target }}) => void
。
所以要设置文本你必须做
onSubmitEditing={(val) => setSearch(event.nativeEvent.text)}
我正在尝试在返回时获取文本输入,这样我就不必在每个键输入上连续 运行 和 API。当我在 onChangePress 上 运行 时代码有效,但是当我将其更改为 onSubmitEditing 时,我在代码下方收到错误。
export default function Search({navigation}) {
const [search, setSearch] = useState('123')
return (
<View style={styles.container}>
<SafeAreaView>
<View style={styles.header}>
<TouchableOpacity
style={styles.back}
onPress={() => navigation.goBack()}
>
<Ionicons name="arrow-back-circle-sharp" size={24} color="black" />
</TouchableOpacity>
<View style={styles.input}>
<Feather
name="search"
size={20}
color="black"
style={{textAlign: 'center'}}
/>
<TextInput
placeholder='Search for channels'
onSubmitEditing={(val) => setSearch(val)}
/>
</View>
</View>
</SafeAreaView>
<Text>{search}</Text>
<StatusBar style="auto" />
</View>
);
}
错误:
Error: Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, _dispatchListeners, _dispatchInstances, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, isDefaultPrevented, isPropagationStopped}). If you meant to render a collection of children, use an array instead.
如 react-native
文档所述 onSubmitEditing
returns 回调对象 ({ nativeEvent: { text, eventCount, target }}) => void
。
所以要设置文本你必须做
onSubmitEditing={(val) => setSearch(event.nativeEvent.text)}