[英]Type '(text: string) => void' is not assignable to type '() => void'
I'm creating a custom component for TextInput.我正在为 TextInput 创建一个自定义组件。 But I've got a problem when I try to pass a function prop to the custom component.
但是当我尝试将 function 道具传递给自定义组件时遇到了问题。 This is the code
这是代码
// Screen.tsx
export const RegisterScreen = ({props}: Props) => {
const [text, setText] = useState("");
const onChangeInputText = (text: string) => setText(text);
return (
<View>
<CustomInput onChangeText={onChangeInputText} text={text} />
</View>
// CustomTextInput.tsx
type Props = {
onChangeText: () => void;
text?: string;
};
export const CustomInput = ({ onChangeText, text }: Props) => {
return (
<TextInput
style={styles.container}
onChangeText={onChangeText}
value={text}
placeholder="Type here"
/>
);
};
With this code, I got this error使用此代码,我收到此错误
Type '(text: string) => void' is not assignable to type '() => void'
I think I know what causes this (it's probably on the type declaration?) but since this is actually my first time trying TypeScript, I can't really figure out how to solve this.我想我知道是什么原因造成的(它可能在类型声明上?)但由于这实际上是我第一次尝试 TypeScript,我真的不知道如何解决这个问题。 Tried googling about this first but I didn't find any error similar to mine.
首先尝试使用谷歌搜索,但我没有发现任何类似于我的错误。
The type definition for this function says "Hey, i need to be passed a string in order to do my job":这个 function 的类型定义说“嘿,我需要传递一个字符串才能完成我的工作”:
const onChangeInputText = (text: string) => setText(text);
But the definition for the onChangeText prop says "I'm not going to pass anything to you":但是 onChangeText 属性的定义说“我不会向你传递任何东西”:
type Props = {
onChangeText: () => void;
text?: string;
};
Your code will actually pass a string through, so you should just need to update the type on the prop.您的代码实际上将传递一个字符串,因此您只需要更新道具上的类型。
type Props = {
onChangeText: (val: string) => void;
text?: string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.