无法将状态从父组件传递到子组件

时间:2020-10-16 01:30:24

标签: reactjs

我反应很新,有人可以帮助我吗?我无法将父组件的状态作为对子组件的支持。我正在使用打字稿。 我的父组件看起来像这样,

import DisplayResults from './results';

interface SearchResultsInterface{
    SearchString: string;
}

export default class Index extends React.Component<{}, SearchResultsInterface> {

    constructor(props: {}){
        super(props);
        this.state = {
            SearchString: 'abcd'
        }
    }

    render() {
        return (
               <DisplayResults SearchString = {this.state.SearchString} />
        );
    }
}

我收到一个错误,无法将类型{ SearchString: string; }分配给类型IntrinsicAttributes & IntrinsicClassAttributes<SearchResults> & Readonly<{}> & Readonly<{ children?: ReactNode; }>。属性SearchString在类型IntrinsicAttributes & IntrinsicClassAttributes<SearchResults> & Readonly<{}> & Readonly<{ children?: ReactNode; }>上不存在。

我的子组件看起来像这样,

export default class SearchResults extends React.Component {
    render() {
        console.log(this.props.SearchString);
        return ();
    }
}

0 个答案:

没有答案