javascript - How to maintain react component state when goback from router? -
i have 2 react components let , b. when shown on page, user changes on page cause states changed inside a. user click button navigate b router.push('/b')
. user click back button , navigate page done router.goback()
. current url updated states gone. how can maintain state when user go a?
i think need enable browserhistory
on router intializing : <router history={new browserhistory}>
.
before that, should require browserhistory 'react-router/lib/browserhistory'
i hope helps !
var browserhistory = require('react-router/lib/browserhistory').default; var app = react.createclass({ render: function() { return ( <div>xxx</div> ); } }); react.render(( <router history={browserhistory}> <route path="/" component={app} /> </router> ), document.body);
another way can try this,
this.context.router.goback()
no navigation mixin required!
edit: update react v15 , reactrouter v3.0.0 (aug 20th, 2016):
var browserhistory = reactrouter.browserhistory; var backbutton = react.createclass({ render: function() { return ( <button classname="button icon-left" onclick={browserhistory.goback}> </button> ); } });
Comments
Post a Comment