繁体   English   中英

类型 '(text: string) => void' 不可分配给类型 '() => any'

[英]Type '(text: string) => void' is not assignable to type '() => any'

我正在尝试使用 reactnativeelements 中的SearchBar如下:


export function EditHash({ navigation, route }: ModifyHashProps) {

    const [searchQuery, setSearchQuery] = React.useState("");

    const handleSearchChange = (text: string) : void => setSearchQuery(text);

    return (
        <SafeAreaView>

                <SearchBar
                    placeholder="type hashtag"
                    lightTheme={true}
                    inputContainerStyle={{
                        backgroundColor: "#FFFFFF",
                        height:30
                    }}
                    containerStyle={{
                        backgroundColor: "#FFFFFF",
                        borderWidth: 1,
                        borderColor: "#E5E6E6",
                        borderRadius: 10,
                        marginLeft: 20,
                        marginRight: 20,
                    }}
                    value={searchQuery}
                    onChangeText={handleSearchChange}
                />

        </SafeAreaView>

    );
}

不幸的是,编译器抱怨onChangeText函数分配:

Type '(text: string) => void' is not assignable to type '((text: string) => void) & ((text: string) => void) & (() => any) & (() => any) & (() => any) & ((text: string) => void) & (() => any) & (() => any) & (() => any)'.
  Type '(text: string) => void' is not assignable to type '() => any'.

根据类型定义,应该没问题:

在此处输入图片说明

我究竟做错了什么?

输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,>

[英]Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型 '(text: string) =&gt; void' 不可分配给类型 '() =&gt; void' 输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> '{ [x: string]: any; 类型的参数 }' 不可分配给“联系人”类型的参数 类型“字符串”不可分配给类型“ImageSourcePropType” 类型“typeof class”不可分配给类型“ComponentType”<any> '</any> React Native/Typescript/Eslint 错误:类型“void”不可分配给类型“CompositeAnimation” 输入'颜色<colorparam> ' 不可分配给类型 'string'</colorparam> 输入&#39;元素| undefined&#39; 不可分配给类型 &#39;FC<any> | 不明确的&#39; 输入'{ rippleColor: any; }' 不可分配给类型 'ColorValue | null | 不明确的' 'string' 类型的参数不能分配给 '{ results: string; 类型的参数; }'.ts(2345)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM