简体   繁体   English

Redux不适用于React路由器

[英]Redux not working with React-router

I'm using the following: 我正在使用以下内容:

react v16.2.0, react-redux v5.0.7, react-router-dom v4.2.2, redux v3.7.2 react v16.2.0,react-redux v5.0.7,react-router-dom v4.2.2,redux v3.7.2

What I am trying to achieve is to update some props from the Auth component, and when the user navigates to /user (Which loads the Userpage component), the modified props should be displayed. 我想要实现的是从Auth组件更新一些道具,并且当用户导航到/user (加载Userpage组件)时,应该显示修改后的道具。 Here is a simplified version of my code: 这是我的代码的简化版本:

In App.js: 在App.js中:

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import Store from './components/Store';
import Home from './components/Home';
import Auth from './components/auth';
import Userpage from './components/Userpage';
import { BrowserRouter as Router, Route } from 'react-router-dom';

class App extends Component {

  render() {
    return (  
      <Provider store={Store}>    
        <Router>
          <div>
            <Route exact path="/" component={Home}/>
            <Route path="/login" component={Auth}/>
            <Route path="/user" component={Userpage}/>
          </div>
        </Router>
      </Provider>
    );
  }
}

export default App;

In Store.js: 在Store.js中:

import { createStore } from 'redux';

const reducer = (state,action) => {
    if(action.type == 'TEST'){
        return Object.assign({},state,{test:action.payload.test});
    } else    
        return state;
}

export default createStore(reducer,{
    test:'DOES NOT WORK',
})

In Auth.js : 在Auth.js中:

import React from 'react';
import { connect } from 'react-redux';
import Userpage from './Userpage';


class Auth extends React.Component {

    componentWillMount() {
        this.props.update('TEST',{test:'WORKS'});
    }

    render() {
        return(
            <div>
                <Userpage/>
            </div>
        );
    }
}

export default connect(
    (store) => {
        return store;
    },
    (dispatch) => {
        return {
            update:(dispatchType, dispatchPayload) => {
                dispatch({type:dispatchType,payload:dispatchPayload});
            }
        }
    }
)(Auth);

In Userpage.js: 在Userpage.js中:

import React, { Component } from 'react';
import { connect } from 'react-redux';

class Userpage extends Component{
    componentDidMount(){
        console.log(this.props.test);
    }

    render(){
            return null;
    }
}

export default connect(
    (store) => {
        return store;
    }
)(Userpage);

Now, when I navigate to /login, the store is updated and test is set to "WORKS". 现在,当我导航到/ login时,商店将更新,并且test设置为“ WORKS”。 But if I navigate to /userpage, console.log(this.props.test) prints "DOES NOT WORK", instead of the updated prop. 但是,如果我导航到console.log(this.props.test)console.log(this.props.test)工作”,而不是更新后的prop。

Just to test, I included Userpage in the render function of Auth and it console logs "WORKS" correctly. 为了进行测试,我将Userpage包含在Auth的呈现功能中,并且该控制台正确记录了“ WORKS”日志。

So why is the redux store apparently being reset to default values on navigation to another page using react-router? 那么,为什么在使用react-router导航到另一个页面时,将redux存储显然重置为默认值? How can I fix this? 我怎样才能解决这个问题?

Found the solution. 找到了解决方案。 Apparently the whole application re-renders when you manually navigate (by typing in the URL in the browser), hence resetting the redux store. 显然,当您手动导航时(通过在浏览器中键入URL),整个应用程序将重新呈现,从而重置redux存储。

Using Link as mentioned in react-router docs or redirecting using this.props.history.push('/user') works without any issues. 使用react-router文档中提到的Link或使用this.props.history.push('/user')重定向都可以this.props.history.push('/user')工作。

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

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