2017-06-29 163 views
-1

我使用反应路由器V4,当我宣布一个途径,我想换一个高阶组件内我的组件,如果我在阵营路由器V4,使用渲染高顺序组件道具

export default hoc(Component) 
使用HOC

然后我把组件放在渲染道具中,它工作。 当我做这个

`<Route exact path="/projects" render={(props) => (withNavHOC(<ProjectsContainer {...props}/>))} />` 

它返回此错误:

Uncaught Error: Route.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object. 

为什么发生?我的特设工作正常,它返回一个有效的反应成分:

`

const withNavHOC = (WrappedComponent) => { 
    return class extends React.Component{ 
     render(){ 
      if(this.props.match.params.id){ 
       console.log("Here"); 
       return(
        <div> 
         <ProjectMenu/> 
         <WrappedComponent {...this.props}/> 
        </div>) 
      } 
      return(
        <div> 
         <Navigation/> 
         <WrappedComponent {...this.props}/> 
        </div> 
      ) 
      } 
     } 
};` 

回答

0

要HOC这当然是一个简单的功能,我们必须通过“组件”,而不是<Component/>