0

我有一个简单的redux样板项目,当我导入顶级(智能)组件时,与内联类声明相比,我遇到了不同的行为。

创建类不会导致错误,但是当我将类提取到单独的文件时,我收到以下错误:

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

我已经尝试内联整个文件并且错误消失了

// == 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>
    );
  }
}
// ============================================================================

这是项目回购(它的超级条纹背)。

  "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"
  },
4

1 回答 1

0

因此,据我了解,这是因为您已经说过propTypes子项和 routerState 是可以预期的,但是在使用组件时您没有将它们用作 propTypes,因为您将 App 组件作为 propType 传递给<Route />组件。

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

我不确定您的 App 组件是否应该具有这些 propType,因为您无法将它们传入。但是您应该能够this.props.children通过将该 propType 更改为PropTypes.node.

您可以通过执行以下操作将道具传递到路由器组件中:

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

其中 'name' 是您想要的参数,并执行: this.props.params.name在 'MyComponent' 中

于 2015-10-18T22:44:39.077 回答