简体   繁体   English

如何使用浏览器后退按钮跳转到Redux状态

[英]How to Jump to Redux State with Browser Back Button

I'm a react beginner and I have a project using React Router (4.2.2), Redux and I've recently added react-router-redux hoping that it would solve my problem. 我是一个反应初学者,我有一个项目使用React Router(4.2.2),Redux和我最近添加了react-router-redux希望它能解决我的问题。 I have components whose redux stored states need to change based on interactions with the browser back and forward buttons. 我有基于与浏览器后退和前进按钮的交互,其redux存储状态需要更改的组件。

For example, I have a screen containing details for an element the user clicked. 例如,我有一个屏幕,其中包含用户单击的元素的详细信息。 If I use the browser back button and navigate to another point in history in which that details view was open, it will only show the most recent element info (due to that the only information in the store) or sometimes no information at all will be passed. 如果我使用浏览器后退按钮并导航到历史记录中打开详细信息视图的另一个点,它将仅显示最新的元素信息(由于该商店中的唯一信息)或有时根本没有信息通过。

I thought react-router-redux would help me keep these two in sync but maybe I'm missing a step that enables that to happen. 我认为react-router-redux会帮助我保持这两个同步,但也许我错过了一个让它发生的步骤。 I've installed Redux Debug Tools and I can see in there the state that I want to jump to, but how do I enable it when the user uses the back button? 我已经安装了Redux调试工具,我可以在那里看到我想跳转到的状态,但是当用户使用后退按钮时如何启用它? The url for the details page is the same each time it's viewed, perhaps I need to add a hash tag with specific information but even then, how would I do a look up for the correct information in the store? 每次查看详细信息页面的网址都是相同的,也许我需要添加一个包含特定信息的哈希标记,但即便如此,我如何在商店中查找正确的信息?

I open the details view like this: 我打开这样的详细信息视图:

<Link to='/activityDetails'>
       <ActivityButton 
           id={this.props.someData.someId}
       />
</Link>

And inside ActivityButton: 在ActivityButton里面:

openActivityDetails = () => {
    this.props.showActivityDetailsScreen(this.props.id);
};

function matchDispatchToProps(dispatch) {
    return bindActionCreators({
        showActivityDetailsScreen: activityDetailsActions.showActivityDetails
    }, dispatch)
}

render(){
    return (
        <div className={'activity-button'} onClick={this.openActivityDetails}>
            <img
                onClick={this.handleClick}
                src={imgPath}
            />
        </div>
    );
}

So I figured it out on my own. 所以我自己想出来了。 Essentially what you need to do is set the state information inside the when you create it, like so: 基本上你需要做的是在创建时设置状态信息,如下所示:

<Link to={{
    pathname: '/activityDetails',
    state: { "activityData": activityData }
}}>
    <ActivityButton 
        id={this.props.someData.someId}
    />
</Link>

then in the class you need the information ActivityDetails in my case you can query it like this: 然后在类中你需要信息ActivityDetails在我的情况下你可以像这样查询:

 var actData = this.props.location.state.activityData;

or if you're using withRouter and history it's also in the following location: 或者如果您使用的是路由器和历史记录,它也位于以下位置:

const { history: { push } } = this.props;
history.state.state.activityData

I ended up removing react-router-redux because it wasn't necessary. 我最终删除了react-router-redux,因为没有必要。 It made an entry in the state with the current route, but in my case that wasn't useful. 它在当前路线的状态下进入,但在我的情况下没有用。 Hope this helps someone in the future. 希望这可以帮助将来的某个人。

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

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