向减速器调度动作时无法更新状态

时间:2020-01-14 06:39:01

标签: reactjs redux redux-thunk

我在我的 Home 组件上,需要显示文章供稿,为此,我必须具有 articleList 数组。但是出于某种原因,当我进入商店时, articleList 为空。另外,我在获取数据后放置的 console.log 也不起作用。这一切似乎很奇怪。

Home.js


import React, { Component } from "react"
import { connect } from "react-redux"
import { listAllArticles } from "../actions/articles"

class Home extends Component {

    componentDidMount() {
        this.props.dispatch(listAllArticles)
    }

    render() {
        console.log(this.props)
        return (
            <div style={{ textAlign: "center" }}>
                <h1>Conduit</h1>
                <h5>A place to share your knowledge</h5>
            </div>
        )
    }
}

const mapStateToProps = (state) => {
    return state
}

export default connect(mapStateToProps)(Home)
listAllArticles


export const listAllArticles = () => {
    console.log("inside listAllArticles action creator")
    return dispatch => {
        fetch("https://conduit.productionready.io/api/articles")
            .then(res => res.json())
            .then(data => {
                console.log(data.articles)
                dispatch({
                    type: "LIST_ALL_ARTICLES",
                    data: data.articles
                })
            })
    }
}
articleReducer


const initState = {
    articleList: null
}


export const articleReducer = (state=initState, action) => {
    console.log("inside article reducer")
    switch(action.type) {
        case "LIST_ALL_ARTICLES":
            return {...state, articleList: action.data}
        default:
            return state
    }
}

0 个答案:

没有答案
相关问题