我有一个用react和redux构建的简单的"post-like"应用程序。当用户登录时,他应该看到一个导航栏,当他没有登录时,应该看到另一个。
我使用reduxdevtools来跟踪状态的变化。
问题是,当我登录时,我设置了一个本地存储令牌,当我注销时,我将该令牌设置为null。这种方法效果很好,当我点击注销时,我在redux devtools中看到令牌为null
和user_authenticated
为false,但当我返回主页或点击浏览器返回按钮时,它会恢复到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"
我的一些组件如下所示:
routes.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;
Index.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;
}
}
好吧,经过几天的调试,从头到尾都在学习redux教程,但仍然失败了。
我终于意识到,我正在返回initialState以进行默认操作。因此,当加载新组件时,它将获得initialState,而不是更新状态。