React v15.4到v16迁移问题

时间:2017-10-10 18:57:22

标签: reactjs react-router material-ui

我尝试迁移整个项目,并且我在main.jsx文件中遇到此问题。

  • " react-router":" 3.0.x"
  • " material-ui":" ^ 0.19.4"
  • " react-redux":" 4.4.8"
  

对象无效作为React子对象(找到:带键的对象)   {默认})。如果您要渲染一组孩子,请使用   数组代替。       在路由器中(在main.jsx:68)       在MuiThemeProvider中(在main.jsx:67)       在提供者(在main.jsx:66)

代码

ReactDOM.render(
    <Provider store={store}>
        <MuiThemeProvider muiTheme={getMuiTheme(MyRawTheme)}>
            <Router
                history={history}
                routes={routes}
                render={applyRouterMiddleware(useScroll())}
            />
        </MuiThemeProvider>
    </Provider>, document.getElementById('main-app')
);

什么是布莱恩?

2 个答案:

答案 0 :(得分:0)

看起来React Router v3.0.x不支持React 16.尝试升级到React Router v3.2.x,尽管我推荐使用React Router v4。有关详细信息,请参阅此thread

您似乎正在导出对象而不是class。而不是const routes是一个React元素,而是创建一个Routes组件(函数或类)并将所有路由放在其render()中。
例如:

const Routes = () => (
  <Route>
  .
  .
  .
  </Route>
)

并在主文件中:

<MuiThemeProvider muiTheme={getMuiTheme(MyRawTheme)}>
  <Router
    history={history}
    render={applyRouterMiddleware(useScroll())}>
      {routes}
  </Router>
</MuiThemeProvider>

答案 1 :(得分:0)

I finally managed to fix everything successfully.

The code was using requires everywhere (even in routes) so I had to change all those requires into import or require('path').default; since the new webpack creates an object which has the actual required file inside into the key 'default'.