0

我有一个用 react 和 redux 构建的简单的“post-like”应用程序。当用户登录时,他应该看到一个导航栏,当他没有登录时,他应该看到另一个。

我正在使用 redux-devtools 来跟踪状态变化。

问题是,当我登录时,我设置了一个本地存储令牌,而当我注销时,我将该令牌设置为空。这种方法效果很好,当我单击注销时,我在 redux devtools 中看到令牌是假的nulluser_authenticated但是当我返回主页或单击浏览器后退按钮时,它会恢复到login状态,然后我可以查看令牌值并is_authenticated设置为 true。

当我刷新页面时,我得到了我想要的行为,即用户注销,令牌设置为 null,然后is_authenticated设置为 false。

奇怪的是,如果我这样做console.log(store.getState()),我会在那里获得更新/预期的状态,但 devtools 显示以前的状态,页面甚至表现得像以前的状态。

登录也是如此,在导航中来回切换会让我回到我第一次访问该页面时的旧状态。

退出后返回主页后,从我的 redux-devtools 中状态。

user: {
    is_authenticated: true,
    token: 'c80c632a6529b79d51fd3e2fc915cfb94834aa5237b4e65924c095ea58f289b3',
    is_loading: false
  },

store.getState()从注销移回后,在同一页面上的状态

user:
is_authenticated: false
is_loading: false
token: "null"

下面列出了我的一些组件:

路由.js

import { Switch, Route } from 'react-router-dom';
import React, {Component} from 'react';
import Homepage from './components/homepage';
import Scammer from './components/scammer';
import ScammerCreate from './components/scammer_create'
import Navbar from './components/navbar'
import Register from './components/register'
import Login from './components/login'
import Logout from './components/logout'
import {store} from './index'




class Routes extends Component {

    componentWillMount() {
        store.dispatch({
            type: 'get_user',
        })
        store.getState()
    }
    

        render(){
            return(
                <div>
    
                
                <Navbar />
                <Switch>
                    <Route path="/" exact component={Homepage}/>
                    <Route path="/scammer/create" exact component={ScammerCreate}></Route>
                    <Route path="/scammer/:id" exact component={Scammer}/>
                    <Route path="/auth/register" exact component={Register}></Route>
                    <Route path="/auth/login" exact component={Login}></Route>
                    <Route path="/auth/logout" exact component={Logout}></Route>
                    
                </Switch>
                </div>
            )
        }
}

export default Routes;

索引.js


import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';

import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import promiseMiddleware from 'redux-promise';
import thunkMiddleware from 'redux-thunk'
import Routes from './routes'

import rootReducer from './reducers/index';

//const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore)
const middleware = [promiseMiddleware,thunkMiddleware]
export const store = createStore(
    rootReducer,
    composeWithDevTools(applyMiddleware(...middleware))
)


ReactDOM.render(
    <Provider store={store}> 
        <BrowserRouter>
            <Routes/>
        </BrowserRouter>
    </Provider>
, document.getElementById('root'));


user_reducer



let initialState = {

}
if (localStorage.getItem('token') != null && localStorage.getItem('token') != 'null'){
    initialState['is_authenticated'] = true;
    initialState['token'] = localStorage.getItem('token');
    initialState['is_loading'] = false;
}

else{
    initialState ={
        'is_authenticated' : false,
        'token' : localStorage.getItem('token'),
        'is_loading' : false,
    }
    
}

export default function user(state=initialState,action){

    if (action.type == "registered") {
        localStorage.setItem('token',action.payload.token);
        return {...state, 'user_details': action.payload.user,'token':localStorage.getItem('token'),'is_authenticated':true,'is_loading':false}

    }

    else if ( action.type == "login" ){
        
        console.log(action.payload)
        localStorage.setItem('token',action.payload.token)
        return {
            ...state,'is_authenticated' : true, 'token' : localStorage.getItem('token'),'is_loading' : false,
            'user_details' : action.payload.user,
        }
    }

    else if( action.type == "logout" ){
        localStorage.setItem("token",null);
        return {
            ...state,'is_authenticated' : false, 'token' : localStorage.getItem('token'), 'is_loading' : false
        }
    }

    else if(action.type == 'get_user'){
       const token = localStorage.getItem('token');
       console.log(token)
       if (token != null && token != 'null'){
           return {
               ...state,'is_authenticated' : true, token : token, is_loading : false,
           }

        }
        else {
            return {
                ...state, is_authenticated : false, token: token, is_loading : false,
            }
    
    }
    }

    else{
        return initialState;
    }


}
4

1 回答 1

1

好的,经过几天的调试,并从头到尾浏览了 redux 教程,但仍然失败。

我终于意识到我正在为默认操作返回初始状态。所以当一个新组件加载时,它会得到初始状态而不是更新状态。

于 2020-09-24T14:23:12.950 回答