我正在尝试通过父组件的连接组件(容器)从子组件调用函数。
当我尝试引用子组件时出现错误Property 'ref' does not exist on type 'IntrinsicAttributes & Pick<{ results: {}[]; }, never>'.
对不起,即将出现的代码墙,我试图尽可能地修剪代码。
这是父组件:
imports...
class App extends React.Component {
searchBoxRef = createRef<SearchBox>();
resetTextBox() {
console.log("It's happening!")
// if (this.searchBoxRef.current) {
// this.searchBoxRef.current.clearInput();
// }
}
render() {
return (
<div>
<div className="grid-container">
<div className="header">
<SearchBoxContainer ref={this.searchBoxRef} />
</div>
<FacetboxContainer resetAll={this.resetTextBox} />
<ResultsContainer/>
</div>
</div >
)
}
}
export default App;
容器组件:
imports...
function getReturnType<RT>(expression: (...params: any[])=>RT): RT {
return {} as RT;
}
declare type SearchDispatchActions = FacetsAction | SuggestionsAction | InputAction
const mapDispatchToProps = (dispatch: redux.Dispatch<SearchDispatchActions>) => {
return {
*dispatch items*
}
}
function mapStateToProps(state: Store.SearchState) {
return {
*props*
}
}
export const stateProps = getReturnType(mapStateToProps);
export const dispatchProps = getReturnType(mapDispatchToProps);
export type PropsType = typeof stateProps & typeof dispatchProps;
type State = {};
const SearchBoxContainer = connect(mapStateToProps, mapDispatchToProps, null, { forwardRef: true })(SearchBox);
export { SearchBoxContainer }
最后是 Searchbox 组件
imports...
export type State = {
searchInput: string;
};
class SearchBox extends React.Component<PropsType, State> {
constructor(props: PropsType) {
super(props);
this.state = { searchInput: "" };
}
private searchRef = React.createRef<HTMLInputElement>();
...
render() {
...
return (
...
);
}
}
export default SearchBox;
谢谢您的帮助。