为什么响应良好,但她没有注入 Redux 状态



你好,我开始了,我来问我的第一个问题。

的确,我想把信息注入Redux的状态。

export const GET_USER = "GET_USER";
export const getUser = () => {
const newVar = async (dispatch) => {
await fetch (${process.env.REACT_APP_API_URL}/api/user/me, {
method: "GET",
headers: {
'Authorization': 'Bearer ' + localStorage.token,
'User': localStorage.user,
},
credentials: "same-origin",
})
.then((response) => {
console.log( 'user get  :'+response)
dispatch({type:GET_USER, payload: response})
})
.catch((error) => console.log(error))
};
return newVar
}

我得到以下结果:

在我的控制台的响应:

XHRGEThttp://localhost:8000/api/user/me
[HTTP/1.1 304 Not Modified 7ms]

lastname    Ceasar
firstname   Jules
email   "jc@mail.com"
cellphone   0505050505
id  "57e925c0-5619-492c-8640-a8102a40bcbf"

在Redux状态(i don't have data):

userReducer(pin): {}

Console.log:

console.log ('user get:' + response) => user get: [object Response]
console.log (response) => does not appear

我搜索给数据一个Redux状态的解决方案。如果你有的话,谢谢你的指导:)

的好日子

假设您的api返回json,那么根据文档,您必须在响应上使用response.json()。所以应该是:fetch().then(respose=>response.json()).then(result=>dispatch...)

你的reducer中的代码也可能是错误的,但只能猜测,因为你的问题中缺少该部分。

最新更新