使用react+redux
登录成功后用户信息存在redux中,
用axios封装一个请求http.js,以后每次请求都带自动带上登录成功的token, 如下:
import store from '@/redux/store';
export const get = (obj) => {
let token = store.getState().userRe.token || '';
axios({
...obj,
method: 'get',
url: obj.url,
params: {token},
}).then(() => mull).catch(() => null)
}
我在react-native上这样使用是没问题的,
但是在react中,http.js引入store则会以下错误.
Error: Reducer "userRe" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.
人到中年有点甜
相关分类