简体   繁体   English

反应路由器不渲染组件

[英]React router not rendering component

Here is the code from my App.js 这是我的App.js中的代码

  render() {
    return (
      <Router history={hashHistory}>
        <Route path='/' component={()=>{return <Container videoConf = {this.state.videoConferenceCapable}/>}}>
          <IndexRoute component={SourcesContainer} />
          <Route path='/videoConference'  component={()=>{return <VideoConference deviceID="57ab270b59edc845274aae09"/>}} />
        </Route>
      </Router>
    )
  }

And from Container.js 从Container.js

export default class Container extends React.Component {

  render() {

  const NavBar = this.props.videoConf ? <div><Nav/></div> : <span></span>
    return (
      <div className="container">
        {NavBar}
        <div>
          {this.props.children}
        </div>
      </div>
    );
  }
}

The problem I'm having is that the Container component is not being rendered and subsequently none of it's children are either. 我遇到的问题是容器组件没有被渲染,因此子组件都不是。 If I remove the Container component then the VideoConference component loads without a problem. 如果删除了Container组件,则VideoConference组件将加载而不会出现问题。 Therefore I am led to assume the issue lies within the way the Container component is being rendered. 因此,我被假定为问题出在渲染容器组件的方式之内。 Appreciate any help. 感谢任何帮助。

I never see this component instantiation inside of router 我从未在路由器内部看到此组件实例化

 <Route path='/' component={()=>{return <Container videoConf = {this.state.videoConferenceCapable}/>}}>

Router component is top-level component, that require only props from url, try to do this 路由器组件是顶级组件,仅需要来自url的prop,请尝试执行此操作

  <Route path='/' component={Container}>

Otherwise look like you have some problems with application logic. 否则,看起来您在应用程序逻辑方面存在一些问题。

Try to do this one: 尝试执行以下操作:

export default Application extends React.Component{
static instance;
constructor() {
    Application.instance = this;
}
render(){
   return <Router />
}
}

and try to access application properties from Container using static instance. 并尝试使用静态实例从Container访问应用程序属性。 Otherwise you can assign something like 'context' object, that will be accessible to all components. 否则,您可以分配“上下文”对象之类的东西,所有组件都可以访问。

If you're trying to pass props to child routes, you can do it this way from the top level component. 如果您尝试将道具传递到子路线,则可以从顶层组件通过这种方式进行。

Change 更改

{this.props.children}

to

{React.cloneElement(this.props.children, { videoConf : this.state.videoConferenceCapable })}

But note that this prop will be passed to all children 但请注意,该道具将传递给所有儿童

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM