gpt4 book ai didi

javascript - Redux 正确发送有效负载

转载 作者:行者123 更新时间:2023-11-29 20:31:34 24 4
gpt4 key购买 nike

我是 Redux 的新手。我正在尝试创建一个简单的 FETCH_ALL_POSTS

Action

export const fetchPosts = () => async dispatch => {
const response = await jsonPlaceholder.get('/posts');
console.log(response.data)
dispatch({
type: FETCH_ALL_POSTS,
payload: response.data
})
}

posts reducer

export default (state = {}, action) => {
const { type, payload } = action;
switch (type) {
case FETCH_ALL_POSTS:
return {
...state, payload
}
default:
return state
}
}

发布列表组件

const mapStateToProps = state => {
console.log(Object.values(state.posts))
return {
posts: state.posts
}
}

这是有效的,但我从 mapStateToProps 获得的数据不是我所期望的。

结果:“数组:[ 0:[{},{},{}] ]”

我的预期结果:“array:[{},{},{}]”

最佳答案

试试这个,

    const initialState = {
posts: '',

}
export default (state=initialState, action) => {
const { type, payload } = action;
switch (type) {
case FETCH_ALL_POSTS:
return{
posts:state.posts=action.payload.posts
}
default:
return state
}
}

关于javascript - Redux 正确发送有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57881017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com