[英]React native text input synthetic event issue
我在本机反应中制作了一个可重用的文本输入组件,但是当我尝试访问文本值时,它给了我合成事件对象然后是文本值,我无法定位文本值。 我知道这可能是重复的,但我尝试了很多方法,但它不起作用。 这是我的代码片段。
/**导入组件**/
<InputField
placeholder="Disabilities"
defaultValue={values.associatedDisabilities.toString()}
onChange={(value) => console.log(value)}
/>
/**可重用组件**/
<TextInput
style={[
styles.textInput,
props.style,
{ color: theme.colors.mainForeground },
]}
onChangeText={props.onChange}
defaultValue = {props.defaultValue}
value={props.value}
onBlur={props.onBlur}
{...props}
placeholderTextColor={
currentTheme === ThemeTypes.LIGHT
? theme.colors.secondaryText
: theme.colors.mainForeground
}
editable={props.editable}
selectTextOnFocus={props.editable}
/>
您需要将它传递给 onChange 以获得您正在寻找的值:
<TextInput
style={[
styles.textInput,
props.style,
{ color: theme.colors.mainForeground },
]}
onChangeText={(value) => props.onChange(value)}
defaultValue = {props.defaultValue}
value={props.value}
onBlur={props.onBlur}
{...props}
placeholderTextColor={
currentTheme === ThemeTypes.LIGHT
? theme.colors.secondaryText
: theme.colors.mainForeground
}
editable={props.editable}
selectTextOnFocus={props.editable}
/>
其次,您需要控制导入的 comp:
<InputField
placeholder="Disabilities"
defaultValue={values.associatedDisabilities.toString()}
onChange={(value) => console.log(value)}
value
/>
否则你可以使用event.target.value
,即输入组件的原生事件值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.