redux - Redux-reducer 没有被调用
问题描述
我正在尝试学习 Redux。我正在尝试一个测试应用程序,但我遇到了这个错误,不知道减速器为什么不更新状态。我已经查看了导致此问题的常见问题,但似乎在我的错误中找不到这些错误,我的减速器没有改变状态
logItem.js
import React from 'react';
import {Link} from 'react-router-dom';
import {connect} from 'react-redux';
import {addCurrent} from '../../actions/logAction';
const LogItem = (props) => {
const {message, tech, date, attention} = props.singleLog;
console.log(props.currentLog)
const updateLog = (singleLog) => {
updateLog(singleLog, singleLog._id)
}
const current = (singleLog) => {
console.log(singleLog)
addCurrent(singleLog)
}
return (
<div>
<h3>{message}</h3>
<p className={`text ${attention}`}>was last updated by {tech} on {date}</p>
<Link onClick={() => current(props.singleLog)} to='/addLog'>update</Link>
<button>delete</button>
</div>
)
}
const mapStateToProps = (state) => ({
currentLog : state.log.current
})
export default connect(mapStateToProps, {addCurrent})(LogItem);
logaction.js
export const addCurrent = (singleLog) => {
console.log(singleLog)
return({type: SET_CURRENT, payload: singleLog})
}
import { SET_LOADING, GET_LOGS, LOGS_ERROR, ADD_LOG, UPDATE_LOG, SET_CURRENT } from '../actions/types';
const initialState = {
logs: null,
loading: true,
current: null,
errors: null
}
logReducer.js
import { SET_LOADING, GET_LOGS, LOGS_ERROR, ADD_LOG, UPDATE_LOG, SET_CURRENT } from '../actions/types';
const initialState = {
logs: null,
loading: true,
current: null,
errors: null
}
export default (state = initialState, action) => {
console.log(action.type)
switch(action.type) {
case SET_CURRENT:
console.log("5")
console.log(action.payload)
return {
...state,
current: action.payload,
errors:null
}
default:
return state;
}
}
解决方案
你的动作没有被调度,不知道为什么你声称减速器没有做任何事情,而显然这个动作甚至没有被调度。下次请使用 redux devtools,这样你至少知道发生了什么,并且可以提出更好的问题。
你应该替换addCurrent(singleLog)
为props.addCurrent(singleLog)
推荐阅读
- javascript - 带有反应比较示例的 jquery
- regex - nginx 位置正则表达式设置仅在指定的 url 上返回 404
- javascript - 如何在 JS 中过滤多个字符串?
- c# - LiveChart 不显示 x 轴 WPF 上的所有标签
- c - 我想让我的素数测距仪程序在 C 中更快
- angular - Firestore - 用户最喜欢的数据结构建议
- fedora - 无法在 Fedora 28 中构建 gnome-system-tools [glib-compile-schemas not found]
- java - 成功后步骤未能在 travis-ci 构建中找到项目根目录下的文件
- angular - 订阅有效但仍然显示错误?
- python - 修复 PyQt5 中 QTableView 中的 Add Row 函数