![](/img/trans.png)
[英]Passing props to a React Redux connected component wrapped in HOC
[英]Injecting/passing components as props from HOC to the Wrapped-Component
在我最近的React項目中,我使用了一些HOC將組件作為道具傳遞給包裝的組件,我想知道這種方法是否有什么問題。 下面是一個示例:
HOC:
import AnotherComponent from './a'
function withExampleComponent(WrappedComponent){
render(){
const exampleComp = <ExampleComponent someprops={value} />
return(
<WrappedComponent {...this.props} exampleComponent={exampleComp} />
)
}
}
這是使用上述HOC的組件
class MainComponent extends React.Component{
render(){
const {exampleComponent} = this.props
return(
<div>
{exampleComponent}
</div>
)
}
}
export default withExampleComponent(MainComponent)
謝謝
在withExampleComponent中,您缺少返回組件定義的信息。 根據反應站點https://reactjs.org/docs/higher-order-components.html 。 你應該返回類似
import AnotherComponent from './a'
function withExampleComponent(WrappedComponent){
return class extends React.Component {
render(){
const exampleComp = <ExampleComponent someprops={value} />
return(
<WrappedComponent {...this.props} exampleComponent={exampleComp} />
)
}
}
}
並與MainComponent一起使用
export default withExampleComponent(MainComponent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.