小编Ty *_*opp的帖子

React/Redux:为什么 mapStateToProps() 会使我的数组存储状态消失?

在我的商店中,我有一个具有以下形状的状态:{posts: [{...},{...}]},但是当我mapStateToProps()在 Home.js 中使用时,状态返回{posts: []},带有一个空数组(商店状态中曾经有一个数组)。

我是否使用mapStateToProps()不正确或者问题是否源于 Redux 周期的其他部分?

我正在使用的 API fetch,暂时位于 actions.js 中

// api

const API = "http://localhost:3001"

let token = localStorage.token
if (!token) {
    token = localStorage.token = Math.random().toString(36).substr(-8)
}

const headers = {
    'Accept': 'application/json',
    'Authorization': token
}

// gets all posts
const getAllPosts = token => (
    fetch(`${API}/posts`, { method: 'GET', headers })
);
Run Code Online (Sandbox Code Playgroud)

动作和动作创建者,使用 thunk 中间件:

// actions.js

export const REQUEST_POSTS = 'REQUEST_POSTS';
function requestPosts (posts) { …
Run Code Online (Sandbox Code Playgroud)

redux redux-thunk react-redux

0
推荐指数
1
解决办法
2113
查看次数

标签 统计

react-redux ×1

redux ×1

redux-thunk ×1