`react-router`警告解决方案并将根道具传递给子路由

时间:2018-01-30 12:42:10

标签: javascript reactjs react-router

有时我看到了众所周知的警告browser.js:49 Warning: [react-router] You cannot change <Router routes>; it will be ignored,我发现了两个趋势问题,朋友们讨论过这个问题,解决方案是const路由组件并将它们放在Router组件中。

https://github.com/ReactTraining/react-router/issues/2704

https://github.com/reactjs/react-router-redux/issues/179

如下所示:

您会看到使用此代码发出警告:

class Root extends Component {
  render() {
    return (
      <Router history={browserHistory} createElement={this.createElement}>
        <Route component={App}>
          <Route path="/" component={MainPage}/>
          <Route path="/page2" component={Page2}/>
          <Route path="/settings" component={SettingsPage}/>
        </Route>
      </Router>
    )
  }
}

但您不会看到此代码的警告:

const routes = (
  <Route component={App}>
    <Route path="/" component={MainPage}/>
    <Route path="/page2" component={Page2}/>
    <Route path="/settings" component={SettingsPage}/>
  </Route>
);

class Root extends Component {
  render() {
    return (
      <Router history={browserHistory} createElement={this.createElement}>
        {routes}
      </Router>
    )
  }
}

这是一个很好的解决方案,可以消除[react-router]警告,对于Root Component更改stateroutes是静态的,你不会看到任何警告。 但是 我的问题是:我将Root Component道具传递给每个Route,我无法执行上述解决方案, 我必须将App Route放在路由器中,所以这个方法绝对不是解决方法,我会再次看到已知的警告,请参阅我的路由器代码:

export default class AppRoutes extends Component {
    render() {
        return (
            <Router history={hashHistory}>
                <Route path="/" {...this.props} component={App}>
                    <IndexRoute component={Home}  {...this.props}/>
                    <Route path="/transaction" component={Transaction} {...this.props}/>
                    <Route path="/users-management" component={UsersManagement} {...this.props}/>
                    <Route path="/issues" component={Issues} {...this.props}/>
                    <Route path='/not-found' component={NotFound}/>
                    <Route path='/settlement-management' component={SettlementManagement} {...this.props}/>
                    <Route path='/categories-management' component={CategoriesManagement} {...this.props}/>
                    <Route path='/gifts-management' component={GiftsManagement} {...this.props}/>
                    <Redirect from='/*' to='/not-found'/>
                </Route>
            </Router>
        );
    }
}

Root Component呈现代码为:

render(){
    return(
        <AppRoutes {...this}/>
    );
}

我将此作为道具传递给AppRoutes组件,我需要将继承的this.props传递给子Route并使用它们。我怎么能看到警告并将道具传递给任何Route

我的一个解决方案是,我将所有Route写为静态,并在每个组件内直接调用Root Component props,但是如何?我不知道如何调用并props Root Component props Root Component Root Component get() $arr = []; Log::info(json_encode($request->get('emptyArray',$arr))); {{1}} {{1}} {{1}}孩子们?

1 个答案:

答案 0 :(得分:0)

您可以使用render路线道具代替component将道具传递给您的组件:

<Route path="/transaction" render={() => <Transaction {...this.props} />} />

编辑:或者这也传递路径道具:

<Route path="/transaction" render={(routeProps) => <Transaction parentProps={this.props} {...routeProps}  />} />

(我认为最好将单个自定义父道具传递给不与routeProps冲突)