javascript - Reverse state on browser back button in reactjs - Stack Overflow

For a consistent user experience, I'd like my react app to reverse its state when the user clicks

For a consistent user experience, I'd like my react app to reverse its state when the user clicks the browser back button.

For example, if I change an element in the state with a function call:

  changeVar = (idx) => (event) => {
    this.setState({thisIndex: idx});
    //how to notify browser stack of this change?
  }

How could I add this to the browser stack so that users can click the back button in the browser to reverse the state change?

For a consistent user experience, I'd like my react app to reverse its state when the user clicks the browser back button.

For example, if I change an element in the state with a function call:

  changeVar = (idx) => (event) => {
    this.setState({thisIndex: idx});
    //how to notify browser stack of this change?
  }

How could I add this to the browser stack so that users can click the back button in the browser to reverse the state change?

Share Improve this question asked May 20, 2018 at 15:24 bearbear 6631 gold badge15 silver badges34 bronze badges 1
  • 2 If you are using redux (and you probably should if you aren't), the topic is covered [here][1] [1]: redux.js/recipes/implementing-undo-history – Barazu Commented May 20, 2018 at 20:20
Add a ment  | 

1 Answer 1

Reset to default 4

So in general question is "how could I handle browser Back/Next button clicked?" or "How could I react on user navigates through history?"

Here such a thing like History API with its .pushState es to the scene.

In short:

  1. on each significant state change you call history.pushState(stateData)
  2. once user clicks Back button it does not actually navigate away to different URL rather change current state only(but only if previous state in the stack has been created with .pushState or .replaceState)
  3. your code detects if history navigation occurs(Back button been clicked) by listening to popstate event
  4. based on appropriate state data that you read from history.state you restore application state appropriately

So far the last question is how to put all the data across your application in single object and to restore all the application with it modules' states later. And here redux es to the help. Redux suppose you have just single Store over all your application. In other words all the state is already contained in single object that you just need to pass into .pushState() to store and to restore it after Back button is clicked.

PS remember I told about significant change? since "all the data all over the application" is large enough it would make overhead to store store it million times in the memory. Also it would be a silly if clicking Back button say just collapse a dropdown.

PPS and since history is global there is no way to store/restore state on per-ponent way. so maybe(just maybe) it's better to implement something like "state change history" for your ponent and propose user some different control to restore its state(instead of clicking Back button in browser)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745142698a4613502.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信