【发布时间】:2021-02-04 13:52:41
【问题描述】:
我正在编写一个非常简单的反应组件,但我尝试过的所有操作都出现控制台错误。
在我的主渲染中:
render() {
const subreddits = withFetch(new Subreddits({}));
...
{subreddits}
}
子版块:
class Subreddits extends React.Component<Record<string, unknown>, any> {
displayName: string;
constructor(props: any) {
super(props);
this.displayName = "Subreddits";
}
public render() {
const data = this.props.data?.[0];
const options = data?.map((e) => ({
name: e,
value: e,
}));
return (
<SelectSearch
search={true}
options={options}
value={this.props.input.h}
name="reddit select"
placeholder="Select"
onChange={async (handle: string) => {
}}
/>
);
}
}
我将 fetch 修改为更简单:
function withFetch(WrappedComponent) {
return class extends React.Component {
displayName: string;
constructor(props) {
super(props);
this.displayName = "log";
}
componentDidMount() {
console.log("Hello World!");
}
render() {
return <WrappedComponent />;
}
};
}
export default withFetch;
请原谅目前糟糕的打字稿。这是我得到的错误:
Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
in div (created by InputGroup)
in InputGroup (created by App)
in div (created by App)
in div (created by App)
in App (created by HotExportedApp)
in AppContainer (created by HotExportedApp)
in HotExportedApp
好的,我意识到它说它不能渲染函数,但是我打开调试器,它不是函数而是对象。我尝试像函数一样调用它只是为了确定,但绝对不是。这留下了另一个选项if you return a Component instead of <Component /> 但是,对我来说似乎很好。当我注释掉{subreddits} 时,错误就消失了。我犯了什么错误?
【问题讨论】:
标签: javascript reactjs typescript higher-order-components