首页 > 解决方案 > 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

解决方案


你的动作没有被调度,不知道为什么你声称减速器没有做任何事情,而显然这个动作甚至没有被调度。下次请使用 redux devtools,这样你至少知道发生了什么,并且可以提出更好的问题。

你应该替换addCurrent(singleLog)props.addCurrent(singleLog)


推荐阅读