<Route>不显示组件

时间:2019-12-15 10:57:07

标签: reactjs react-router react-router-dom

我有一个MainRouter.js,其中封装了我的所有路线。

<Home>组件呈现,但是Users组件不呈现,并且出现Cannot /GET users错误

MainRouter.js

import React, {Component} from 'react'
import {Route, Switch} from 'react-router-dom'
import Home from './core/Home'
import Users from './user/Users'  
class MainRouter extends Component {
  render() {
    return (
     <div>
      <Switch>
        <Route exact path="/" component={Home}/>
        <Route path="/users" component={Users}/>
      </Switch>
    </div>)
  }
}

export default MainRouter

Users.js./user/Users.js组件下:

// import statements
// ...
class Users extends Component {
  state = { users: []}
  render() {
    const {classes} = this.props
    return (
        <List dense>
         {this.state.users.map((item, i) => {
          return <Link to={"/user/" + item._id} key={i}>

                      <ListItemText primary={item.name}/>
                      <ListItemSecondaryAction>
                      <IconButton>
                          <ArrowForward/>
                      </IconButton>
                      </ListItemSecondaryAction>
                    </ListItem>
                 </Link>
               })
             }
        </List>
    )
  }
}

Users.propTypes = {
  classes: PropTypes.object.isRequired
}

export default withStyles(styles)(Users) 

是客户端渲染问题还是服务器端渲染问题?

1 个答案:

答案 0 :(得分:-1)

尝试在export default withStyles(styles)({Users})中使用语法Users.jsUsers组件应作为对象传递。