Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript can';更改onChangeText后在输入字段中键入_Javascript_Reactjs_Typescript_React Native_Formik - Fatal编程技术网

Javascript can';更改onChangeText后在输入字段中键入

Javascript can';更改onChangeText后在输入字段中键入,javascript,reactjs,typescript,react-native,formik,Javascript,Reactjs,Typescript,React Native,Formik,我有一个自定义的FieldInput图标,其中我使用了Native Base的输入组件。我用它来验证Formik。当我这样使用它时: onChangeText={handleChange(fieldType)} type FieldInputProps = React.PropsWithRef<{ handleChange: (e: string) => void; value: string; fieldType: string; placeholderText

我有一个自定义的FieldInput图标,其中我使用了Native Base的输入组件。我用它来验证Formik。当我这样使用它时:

 onChangeText={handleChange(fieldType)}
type FieldInputProps = React.PropsWithRef<{
  handleChange: (e: string) => void;
  value: string;
  fieldType: string;
  placeholderText?: string;
  style?: object;
  rounded?: boolean;
}>;


export const FieldInput = React.forwardRef<Input, FieldInputProps>(({
  handleChange,
  fieldType,
  placeholderText,
  value,
  style,
  rounded,
}, ref) => {

  return (
    <Item rounded={rounded} style={[styles.inputItem, style]}>
      <Input 
        ref={ref}
        autoFocus={true}
        autoCapitalize="none"
        placeholder={placeholderText}
        keyboardType="default"
        onChangeText={handleChange(fieldType)}
        value={value}
       />
      </Item>
  );
});
一切似乎都很好,但我在onChangeText上发现了一个错误

  Overload 1 of 2, '(props: Readonly<Input>): Input', gave the following error.
    Type 'void' is not assignable to type '((text: string) => void) | undefined'.
错误消失了,但我的输入字段停止工作。我再也打不进去了。我做错了什么?我的组件如下所示:

 onChangeText={handleChange(fieldType)}
type FieldInputProps = React.PropsWithRef<{
  handleChange: (e: string) => void;
  value: string;
  fieldType: string;
  placeholderText?: string;
  style?: object;
  rounded?: boolean;
}>;


export const FieldInput = React.forwardRef<Input, FieldInputProps>(({
  handleChange,
  fieldType,
  placeholderText,
  value,
  style,
  rounded,
}, ref) => {

  return (
    <Item rounded={rounded} style={[styles.inputItem, style]}>
      <Input 
        ref={ref}
        autoFocus={true}
        autoCapitalize="none"
        placeholder={placeholderText}
        keyboardType="default"
        onChangeText={handleChange(fieldType)}
        value={value}
       />
      </Item>
  );
});
类型FieldInputProps=React.PropsWithRef void;
值:字符串;
字段类型:字符串;
占位符文本?:字符串;
风格?:对象;
四舍五入?:布尔值;
}>;
export const FieldInput=React.forwardRef(({
handleChange,
字段类型,
占位符文本,
价值
风格
四舍五入,
},ref)=>{
返回(
);
});

Codesandbox:
onChangeText
是一个接受1个参数
值的函数


onChangeText
(值:string)=>void

因此,您只需要使用
onChangeTextCallback
,如下面的示例所示

 // ✅ Right way to do it!✅ 

const [value, setValue] = useState<string>('')

<TextInput value={value} onChangeText={setValue} />

// or onChangeText={(newValue: string) => setValue(newValue)}

如果对你有帮助,请告诉我!您的fieldType未在arrow函数中定义。试着发送事件。你是说这样的事情
(fieldType)=>handleChange(fieldType)
,我也试过了,但也没用。你能试试密码沙盒吗@金诺诺,这对我没有帮助。我已经在按你的建议做了。即使我使用
onChangeTextCallback
,重载错误仍然存在。你看过我的代码或沙盒了吗?是的,我看过并修复了所有的东西->请让我知道它是否对你有帮助,所以主要的问题是我在onChange上执行
handleChange(fieldType)
,而不是首先将
handleChange('input')
传递给FieldInput组件?我不明白这是怎么解决问题的。它们不是一样吗?正如我在文件
FieldInput.tsx第41行所说,
onChangeText={()=>handleChange(fieldType)}
您没有将
TextInput
value
传递到
handleChange
函数,请检查这里的1个示例