[英]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.