gpt4 book ai didi

javascript - react +还原。我可以通过this.props.children派发一个将包含一个组件并让另一个组件进行渲染的 Action 吗?

转载 作者:行者123 更新时间:2023-11-28 04:49:17 26 4
gpt4 key购买 nike

说我有一个简单的组件。

const SimpleComponent = () => {
return (
<p>Hello!</p>
);
}


如果我将 SimpleComponent导入某个组件,然后分派这样的操作:

this.props.dispatch(someActionOfMine(SimpleComponent));

如何让另一个组件将其内部的组件呈现为 this.props.children

例如,这是 AnotherComponent

class AnotherComponent extends React.Component {
render() {
return (
{React.cloneElement(this.props.children, { ...this.props })}
);
}
}


在某些组件的渲染中可能会这样使用:

render() {
return (
<AnotherComponent>{this.props.somePieceOfState.component}</AnotherComponent>
);
}


能行吗?我正在尝试,但是它提供了此错误:


  invariant.js:44未捕获的错误:元素类型无效:预期为
  字符串(对于内置组件)或类/函数(对于复合
  组件),但得到:未定义。您可能忘记了导出
  定义文件中的组件。

最佳答案

没关系,我是个白痴。您需要像这样传递组件:this.props.dispatch(someAction(<SomeComponent />))

关于javascript - react +还原。我可以通过this.props.children派发一个将包含一个组件并让另一个组件进行渲染的 Action 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43084279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com