【问题标题】:react-router: unable to route to child componentsreact-router:无法路由到子组件
【发布时间】:2018-04-17 02:55:10
【问题描述】:

我正在使用 react 构建一个在线数据库,并且已经设置了路由,如果用户未通过身份验证,他们将被重定向到登录页面。

但是,我面临嵌套路线的困难。我认为实施的某些部分是不正确的。

2 个问题:

  1. 为了让“404”(由 NotFound 组件处理)页面工作,exact props 必须在<Route/> 中使用
  2. 但是如果使用exact props,FixedDrawer 组件中的<Links/> 将不起作用。单击其中任何一个将指向“404”页面。

我在这里做错了什么?

为简单起见,我将只分享组件的渲染部分。

代码如下:

App.js(PageShell 仅用于动画页面过渡)

import LoginPage from "./pages/LoginPage";
import Dashboard from "./pages/Dashboard";
import NotFound from "./pages/NotFound";
.
.
.
render() {
const childProps = {
  isAuthenticated: this.state.isAuthenticated,
  userHasAuthenticated: this.userHasAuthenticated
};

return (
  <div className="App">
    <Switch>
      <Route path="/login" exact component={PageShell(LoginPage)} />
      <Route path="/" exact component={PageShell(Dashboard)} />
      <Route component={NotFound} />
    </Switch>
  </div>
);

仪表板.js

render() {
    return (
      <div>
        <NavBar user={this.state.user} />
        <FixedDrawer handleSetCategory={this.handleSetCategory} />
        <Switch>
          <Route path="/athletes" render={() => <AthletesDataTable />} />
          <Route path="/races" render={() => <RacesDataTable />} />
        </Switch>
      </div>
    );
  }

FixedDrawer.js

class FixedDrawer extends Component {
  constructor() {
    super();
    this.state = {};
  }

  render() {
    return (
      <Drawer variant="permanent" elevation={10}>
        <List component="nav" style={{ top: 65 }}>
          <ListItem button>
            <ListItemIcon>
              <FaceIcon />
            </ListItemIcon>
            <Link to="/athletes" style={{ textDecoration: "none" }}>
              <ListItemText primary="Athletes" />
            </Link>
          </ListItem>
          <ListItem button>
            <ListItemIcon>
              <GroupIcon />
            </ListItemIcon>
            <Link to="/teams" style={{ textDecoration: "none" }}>
              <ListItemText primary="Teams" />
            </Link>
          </ListItem>
          <ListItem button>
            <ListItemIcon>
              <DateRangeIcon />
            </ListItemIcon>
            <Link to="/meets" style={{ textDecoration: "none" }}>
              <ListItemText primary="Meets" />
            </Link>
          </ListItem>
          <ListItem button>
            <ListItemIcon>
              <PoolIcon />
            </ListItemIcon>
            <Link to="/races" style={{ textDecoration: "none" }}>
              <ListItemText primary="Races" />
            </Link>
          </ListItem>
          <ListItem button>
            <ListItemIcon>
              <AssignmentIcon />
            </ListItemIcon>
            <Link to="/results" style={{ textDecoration: "none" }}>
              <ListItemText primary="Race Results" />
            </Link>
          </ListItem>
        </List>
      </Drawer>
    );
  }
}

【问题讨论】:

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


    【解决方案1】:

    “链接”组件将尝试匹配确切的路线。由于“/meets”、“/teams”与你提供的路由不匹配,会给出404页面。

    return (
      <div className="App">
       <Navbar />
       <Sidebar />
       <Switch>
        <Route path="/login" exact component={PageShell(LoginPage)} />
        <Route path="/:val" component={PageShell(SomePage)} />
        <Route path="/" exact component={PageShell(Dashboard)} />
        <Route component={NotFound} />
      </Switch>
      <Footer />
      </div>
    );
    

    我建议你像上面那样编写路由组件。而在“SomePage”组件的componentDidMount中,您可以在“this.props.match”对象中获取上面路径中提到的“val”的值,并根据您想要的路线渲染“SomePage”组件。

    例如:在 SomePage 的 didMount 函数中:

    componentDidMount(){
       this.setState({val: this.props.match.params.val})
    }
    
    render(){
    
      return(
       <div>
         {
         this.state.val === 'teams' ? <TeamsComponent /> : null
         }
         {
         this.state.val === 'meets' ? <MeetsComponent /> : null
         }
       </div>
      )
    }
    

    【讨论】:

    • 我需要在由侧边栏和导航栏包围的仪表板中呈现组件。
    • 答案已编辑,如上所示,您可以通过从 url 获取值、保持侧边栏和导航栏不变来更新 SomePage(在您的情况下为仪表板组件)组件。
    • 我明白了!那么 SomePage 充当某种路由器?
    • 它呈现但我选择放置 <Route/> 的原因在仪表板内是因为我希望在主要内容/数据表发生变化时修复导航栏和侧边栏。此解决方案仅呈现表格组件本身,没有其他内容
    • 为了使导航栏、侧边栏保持不变,您可以将这些组件放在 app.js 中,而不是放在 Dashboard 上。这样,导航栏保持不变,内容会根据您的路线而变化。我已经编辑了上面的答案
    猜你喜欢
    • 2020-01-03
    • 2020-05-24
    • 2021-11-11
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2020-08-12
    • 2017-03-10
    • 2018-01-06
    相关资源
    最近更新 更多