简体   繁体   English

Reactjs与历史

[英]Reactjs with history

I am trying to handle history with reactjs, in such a way that the browser back and forward buttons work intuitively. 我试图用reactjs处理历史记录,这样浏览器的后退和前进按钮就能直观地工作。 Is there any examples of this, or general guidelines I should be following? 有没有这方面的例子,或者我应该遵循的一般准则?

Thanks! 谢谢!

It's not too much different than how you would handle history without React.js. 与没有React.js处理历史记录的方式没有多大区别。 What works well for me is this: I have a top-level component that holds the state of the app in this.state . 对我来说效果很好的是:我有一个顶级组件,它在this.state中保存应用程序的状态。 I'm using Backbone's router , so when I get an event from the router (a URL was loaded or changed in some way), I call setState to update the top-level component's state. 我正在使用Backbone的路由器 ,所以当我从路由器获取事件(URL以某种方式加载或更改)时,我调用setState来更新顶级组件的状态。 When I update the state myself, I make sure to also call the router's navigate method to update the URL. 当我自己更新状态时,我确保也调用路由器的导航方法来更新URL。 Seems to work pretty well: you can see an example from my little app at https://github.com/mjm/eats . 似乎工作得很好:您可以在https://github.com/mjm/eats上看到我的小应用程序中的示例。

Another option with React is to use the React router component. React的另一个选择是使用React路由器组件。 It handles pushState for you and you can dynamically change the available routes as needed. 它为您处理pushState,您可以根据需要动态更改可用的路由。 It works with the same methodology as React does, as it is a react class itself. 它使用与React相同的方法,因为它本身就是一个反应类。 Check it out at: 请查看:

https://www.npmjs.org/package/react-router-component https://www.npmjs.org/package/react-router-component

In the React distribution there's an exemple folder with one being TodoMVC + Director. 在React发行版中有一个exemple文件夹,其中一个是TodoMVC + Director。

Flatiron Director is a router, so you'll have an exemple on how to use it in React. Flatiron Director是一个路由器,所以你将有一个关于如何在React中使用它的例子。

Notice that Instagram is currently using Backbone router but they are planning to use Director (mostly because they can use the same router on client and server side and they don't need Backbone except the router) 请注意,Instagram目前正在使用Backbone路由器,但他们计划使用Director(主要是因为他们可以在客户端和服务器端使用相同的路由器,除路由器外他们不需要Backbone)

In general when it comes to client side route handing, As long as your URL always leads to one application state and one state only the back and forward-buttons will work fine, it shouldn't matter in your application how you reach a certain URL/State. 一般来说,当涉及到客户端路由处理时,只要您的URL始终指向一个应用程序状态和一个状态,只有后退和前进按钮才能正常工作,在您的应用程序中如何到达某个URL无关紧要/州。

So if states in your app (not the React setState-state, just whatever state your app can be in) is always is reflected with a unique URL then you're all set. 因此,如果您的应用中的状态(不是React setState-state,只是您的应用可以处于的任何状态)总是反映在一个唯一的URL中,那么您就已经完成了设置。

I wrote a React mixin for using history.js to push the internal state of a React component to the browser's history. 编写了一个React mixin,用于使用history.js将React组件的内部状态推送到浏览器的历史记录中。

The basic idea is to: 基本思路是:

  1. Register the React component to receive data from the browser history on page load. 注册React组件以在页面加载时从浏览器历史记录接收数据。 This is done with bindToBrowserHistory . 这是通过bindToBrowserHistory完成的。
  2. Call saveState at any point in the React component where you would like to save the state the the browser's history. 在React组件中的任何位置调用saveState ,您希望将状态保存为浏览器的历史记录。
var SimpleCategoryView = React.createClass({
  mixins: [HistoryJSMixin],
  getInitialState: function() {
    return { current_category: 0 }
  },
  handleCategoryChange: function(e) {
    this.setState({current_category: e.target.value});
    this.saveState();
  },
  componentDidMount: function() {
    this.bindToBrowserHistory();
  },
});

In this example I am using handleCategoryChange to handle when the user clicks a button to change the category, when this happens I want to remember the current state of the React component. 在这个例子中,我使用handleCategoryChange来处理当用户单击按钮来更改类别时,当发生这种情况时,我想要记住React组件的当前状态。

I wrote in some more detail about this mixin here . 我在这里写了一些关于这个mixin的更多细节。

Remember EmberJS has done a great work on routing. 记住EmberJS在路由方面做了很多工作。 This react-nested-router has borrowed the technique from that and might help you out. 这个反应嵌套路由器从中借用了这个技术,可能会帮助你。 https://github.com/rpflorence/react-nested-router https://github.com/rpflorence/react-nested-router

React Router has worked really well for me. React Router对我来说非常好用。 Super simple to use, with tons of bells and whistles if you need them. 超级简单易用,如果你需要它们,还有大量的铃声和口哨声。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM