同时反应路由器列表和详细路由

时间:2018-08-04 08:58:06

标签: reactjs react-router react-router-v4

我对React Router 4有问题。我不确定它是否可解决:

这是我的应用程序布局: enter image description here

基本上可以使用。但是问题是,每当我通过左侧链接点击/items/:id/时,它也会与/items匹配。这会导致重新显示侧边栏中的链接列表。解决方案是嵌套路线。但是由于接口/ DOM,这是不可能的。左侧边栏需要与项目详细信息无关。我需要将它们分开:

<div>
  <div className={styles.sidebar}>
    <HeaderContainer />
    <Switch location={location}>
      <Route exact path="/" component={Homepage} />
      <Route path="/items" component={Items} />
    </Switch>
  </div>
  <div className={styles.content}>
    <Route path="/items/:id" component={ItemDetail} />
  </div>
</div>

非常感谢您的提前帮助!

1 个答案:

答案 0 :(得分:1)

我有类似的布局,我使用了类似的东西

//App.jsx
<Router path="/" component={Page}>

//Page.jsx
<Layout>
  <MasterView>
  <DetailView>
</Layout>

//MasterView.jsx
componentDidMount() {
  const { dispatch } = this.props
  const data = await api.getData();
  dispatch(updateDetail(data));
}
connect()(MasterView)

// DetailView.jsx
import { connect } from 'react-redux';

render() {
  return <ul>{this.props.list.map((item) => <li>{item}</li>)}</ul>;
}

// map the props you need to redux state
const mapStateToProps = (state) => ({ list: state.data.list });
connect(mapStateToProps)(DetailView)
相关问题