【问题标题】:`react-router` warning solution and passing root props to sub routes`react-router` 警告解决方案并将根道具传递给子路由
【发布时间】:2018-01-30 12:42:10
【问题描述】:

有一次我看到了众所周知的警告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 更改 state 时,routes 是静态的,您不会看到任何警告。 但是我的问题是:我将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 传递给子Routes 并使用它们。我怎么看不到警告并将道具传递给任何Routes?

我的一个解决方案是,我将所有Routes 写为静态并直接在每个组件内部调用Root Componentprops,但是如何?我不知道如何调用并将propsRoot Component 保留在需要propsRoot Component 的组件内,因为该组件不是直接的Root Component 孩子?

【问题讨论】:

    标签: javascript reactjs react-router


    【解决方案1】:

    您可以使用 render route prop 代替 component 将 props 传递给您的组件:

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

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

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

    (我认为最好是通过单独的自定义父props不要进入与routeProps冲突)

    【讨论】:

    • 我写的和你一样,&lt;Route path="/transaction" render={() =&gt; &lt;Transaction {...this.props}/&gt;}/&gt;,但是没有用。甚至没有像以前那样渲染Transaction
    • 这也不是我的解决方案,因为当我将&lt;Route path="/" {...this.props} component={App}&gt; 及其子项迁移到类外的const 变量时,您的代码风格和我的没有什么不同。课外this 没有意义。
    • 这不是在类之外创建一个 const,而是将 props 传递给你的组件,而不是传递给路由(警告告诉你传递给路由的新 props 被忽略)
    • 这对我没有帮助,我有其他解决方案,我如何在其他组件中直接使用Root Component props?
    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 2016-03-19
    • 2021-04-03
    • 2018-05-19
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多