Type '(event: FormEvent) => void' is not assignable to type '() => any'.
在我的父组件中,我只是将一个处理 onChange 的函数传递给子组件:
<SearchInput handleSearchTyping={this.handleSearchTyping}/>
@bind
handleSearchTyping(event: React.FormEvent<HTMLInputElement>) {
const target = event.target as HTMLInputElement;
const { value: searchText } = target;
const search = (text: string) => {
const { searchList } = this.state;
const searchedCoins = findAsset(text, searchList);
this.setState({ searchList: searchedCoins });
};
const clearSearch = () => {
this.setState({ searchList: this.state.saved });
};
const handleUpdate = (num: number) => (num > 1 ? search(searchText) : clearSearch());
return handleUpdate(searchText.length);
}
child :SearchInput:
import React from 'react'
export const SearchInput = (props: { handleSearchTyping(): void }) =>
<input type="text" placeholder="Search here" onChange={props.handleSearchTyping} />;
还试过:
interface IProps {
handleSearchTyping(): void;
}
export const SearchInput = (props: IProps) =>
<input type="text" placeholder="Search here" onChange={props.handleSearchTyping} />;
传递到 SearchInput
的属性中的正确类型是什么?
最佳答案
啊,刚刚修好了:
import React from 'react'
interface IProps {
handleSearchTyping(event: React.FormEvent<HTMLInputElement>): void;
}
export const SearchInput = (props: IProps) =>
<input type="text" placeholder="Search here" onChange={props.handleSearchTyping} />;
需要使用 event: React.FormEvent<HTMLInputElement>
类型。
关于javascript - 类型 '(event: FormEvent<HTMLInputElement>) => void' 不可分配给类型 '() => any',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54800419/