【问题标题】:Need A React Router Pro to help me需要一个 React Router Pro 来帮助我
【发布时间】:2017-03-09 13:35:30
【问题描述】:

所以今天玩弄了react router,尝试修改this example

我想实现这个:

  • 侧边导航只显示名字

  • 当用户点击名称时,页面会显示名称和所有项目。

例子:

点击饮料,页面会是这样的


饮料

柠檬水

3

根汁啤酒

4

铁港

5


我的问题

app.js

const Item = ({ params: { category, item } }) => {
 const menuItem = data.lookupItem(category, item)

  return (
  <div>
   <h1>{menuItem.name}</h1>
   <p>${menuItem.price}</p>
  </div>
  )
}

我无法理解 params: { category, item } 是什么以及它的来源。

如果您能大致了解解决方案,如果您知道自己在做什么,不到 10 分钟,将非常有帮助和感激。

【问题讨论】:

    标签: javascript api reactjs routing react-router


    【解决方案1】:

    params: { category, item } 来自Router。如以下代码 sn-p,当您使用路由器时,组件将从 Router 获取 params 属性。 params 的值来自路由路径。 Item匹配路径/category/:category/:item,所以{ category, item }的值来自于路由路径对应的位置(:item:category)。

    <Router history={withExampleBasename(browserHistory, __dirname)}>
        <Route path="/" component={App}>
          <Route path="category/:category" components={{ content: Category, sidebar: CategorySidebar }}>
            <Route path=":item" component={Item} />
          </Route>
        </Route>
    </Router>
    

    【讨论】:

    • 天哪,这是一个很好的解释。您介意修改代码以获取我描述的页面吗?
    • @Regex102 对不起。我认为你自己实现它更好。看了react-router的指南,应该不会太难。
    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    相关资源
    最近更新 更多