react jsx语法报错

  1. 报错
 Unexpected token, expected ","
  1. JSX代码
render() {
    const routeInfo = {
      'news': [{
        name: '国内新闻',
        path: 'china',
        component: 'China'
      }, {
        name: '国外新闻',
        path: 'world',
        component: 'World'
      }];
    return (
      <Router>
        <Switch>
          <Route path='/login' component={Login} />
          <Route path='/' render={() => (
            <Container>
              <Switch>
                <Redirect exact path="/" to="/news" />
                {
                  Object.entries(routeInfo).map(([p,r],index1) => {              
                    return (
                      <Route key={`${index1}`} render={() => (
                        <Redirect exact path={`/${p}`} to={`/${p}/${r[0] ? r[0].path : ''}`} />
                        {
                          r.map((i, index2) => (
                            <Route path={`/${p}/${i.path}`} key={`${index1}${index2}`} component={components[i.component]} />
                          ))
                        }
                      )}/>
                    )
                  })
                }
                <Route component={NoFound} />
              </Switch>
            </Container>
          )} />
        </Switch>
      </Router>
    )
  }
  1. 我的哪里的语法有问题么?

环境:
"react": "^16.9.0"
"react-router-dom": "^5.0.1"

阅读 3.5k
2 个回答

这段代码有两个错误,
1.如小石头苦海所说,声明routeInfo变量时,最后缺少一个}
2.在写Route 标签的render属性时,返回的jsx应该是一整个标签,而不能是两个标签

<Route key={`${index1}`} render={() => (
                        <React.Fragment>
                          <Redirect exact path={`/${p}`} to={`/${p}/${r[0] ? r[0].path : ''}`} />
                          {
                            r.map((i, index2) => (
                              <Route path={`/${p}/${i.path}`} key={`${index1}${index2}`} component={components[i.component]} />
                            ))
                          }
                        </React.Fragment>
                        )}/>
const routeInfo = {
  'news': [{
    name: '国内新闻',
    path: 'china',
    component: 'China'
  }, {
    name: '国外新闻',
    path: 'world',
    component: 'World'
  }];

这里就错了吧,最后的;前少了一个}。。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题