简体   繁体   English

在声明的内联中导入时使用相同的JSX组件时出错

[英]Error using the same JSX Component when imported on declared inline

I've got a simple redux boilerplate project and I'm experiencing different behaviour when I import a top level (smart) component compared to if I inline the class declaration. 我有一个简单的redux样板项目,与内联类声明相比,导入顶级(智能)组件时遇到了不同的行为。

Creating the class causes no errors however when I extract the class to a separate file I receive the following error: 创建类不会导致任何错误,但是当我将类提取到单独的文件时,会收到以下错误:

Warning: Failed propType: Required prop `routerState` was not specified in `AppContainer`. Check the render method of `RoutingContext`.

I have tried inlining the entire file and the error goes away 我尝试内联整个文件,但错误消失了

// == Import ==================================================================
import AppContainer from '../containers/App.jsx';
// === Declare Inline =========================================================
import { connect } from 'react-redux';
import { Component, PropTypes } from 'react';


@connect(state => ({ routerState: state.router }))
class AppContainer extends Component {
  static propTypes = {
    children: PropTypes.node.isRequired,
    routerState: PropTypes.object.isRequired,
  }

  render() {
    return (
      <div className="container-fluid">
        <div className="row">
          <div className="col-xs-12">
            <h1>App</h1>
            {this.props.children}
          </div>
        </div>
      </div>
    );
  }
}
// ============================================================================

Here is the project repo (its super striped back). 这是项目存储库 (其超级条带化)。

  "dependencies": {
    "history": "^1.12.5",
    "react": "^0.14.0",
    "react-dom": "^0.14.0",
    "react-redux": "^4.0.0",
    "react-router": "^1.0.0-rc3",
    "redux": "^3.0.2",
    "redux-router": "^1.0.0-beta3"
  },

So from what I understand it's because you have said that the propTypes children and routerState are to be expected, yet you haven't used them as propTypes when using the component, as you are passing the App component as a propType into the <Route /> component. 因此,据我了解,这是因为您曾说过propTypes子级和routerState是预期的,但在使用组件时尚未将它们用作propTypes,因为您将App组件作为propType传递给<Route />组件。

<Route path="/" component={AppContainer}>
  <IndexRoute component={Home} />
</Route>

I am not sure if your App component should have these propTypes, as you can't pass them in. However you should be able to retrieve this.props.children by changing that propType to PropTypes.node . 我不知道,如果你的应用程序组件应该有这些propTypes,因为你不能在通过他们,但你应该能够检索this.props.children通过改变propType到PropTypes.node

You can pass props into your router components by doing: 您可以通过以下方式将道具传递到路由器组件中:

<Route path="myComponent/:name" component={MyComponent} />

Where 'name' is the param you want, and do: this.props.params.name within 'MyComponent' 其中“名称”是您想要的参数,请执行以下操作:“ MyComponent”中的this.props.params.name

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

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