reactjs - 向减速器分派操作时无法更新状态
问题描述
我在我的主页组件上,我需要在其中显示文章提要,为此,我必须拥有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
}
}
解决方案
推荐阅读
- python - 如何连接到 Jupyter Notebook 中的 USB 端口?
- python-3.x - Pandas 根据匹配复制值
- javascript - 是否可以自定义 JS 路径以执行单击元素(复选框) SELENIUM、Java
- python - 使用 zip 在 for 循环中追加 Numpy
- zsh - 当前命令中的 ZSH 反向查找
- javascript - 剑道js jpg到pdf问题
- laravel - 在 Laravel 7 上使用 axios 发布请求
- flutter - 颤振在已知位置的轮播中添加图像
- java - Docker 中的简单 Java 应用程序 - 无法找到或加载主类
- ios - 如何使用本机模块在 iphone 的内部存储中获取所有 pdf 文件以响应本机