javascript 使用 react.js 在单页应用程序中导航

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32030474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:44:37  来源:igfitidea点击:

Navigation in a single page app with react.js

javascriptbackbone.jsreactjssingle-page-application

提问by pdorns

I'm building a single page application with React and Backbone and am trying to figure out best practices for handling navigation between content in the app. My app will have a sidebar with links, for example, to "photos" and "settings". The sidebar is always present, so upon clicking "settings" I want the settings component to be rendered without the entire page reloading. Slack is a great example of what I'm looking for, where clicking a different channel switches the conversation content, but does not reload the entire page.

我正在使用 React 和 Backbone 构建一个单页应用程序,并试图找出处理应用程序内容之间导航的最佳实践。我的应用程序将有一个带有链接的侧边栏,例如“照片”和“设置”。侧边栏始终存在,因此在单击“设置”时,我希望在不重新加载整个页面的情况下呈现设置组件。Slack 是我正在寻找的一个很好的例子,单击不同的频道会切换对话内容,但不会重新加载整个页面。

I had a few ideas on how to implement this, but i'm not sure what's best:

我对如何实现这一点有一些想法,但我不确定什么是最好的:

  1. Have a general react component (console.jsx) that accepts a urlparameter as a prop to determine which content (photos or settings) to render.
  1. 有一个通用的反应组件 (console.jsx),它接受一个 urlparameter 作为道具来确定要呈现的内容(照片或设置)。

or 2. have a general react component and define a state variable that represents which content to render.

或 2. 有一个通用的反应组件并定义一个状态变量来表示要呈现的内容。

Any help would be greatly appreciated!

任何帮助将不胜感激!

采纳答案by Jo?o Lima

Have you heard about react router? Seems to be what you want. react router

你听说过反应路由器吗?似乎是你想要的。 反应路由器