简体   繁体   English

redux mapDispatchToProps 未更新 state

[英]redux mapDispatchToProps not updating state

I am new to redux and encountered an issue with mapDispatchToProps, I have a component in my react app with different divs, every time the user clicks a div it's supposed to change the selected color through an argument, which is then passed down again as a prop.我是 redux 的新手,遇到了 mapDispatchToProps 的问题,我的 react 应用程序中有一个具有不同 div 的组件,每次用户单击 div 时,它应该通过参数更改所选颜色,然后再次作为支柱。

I am getting the prop from the initial state but unable to get the mapDispatchToProps to work properly.我从最初的 state 获得道具,但无法让 mapDispatchToProps 正常工作。 it seems that there are different ways to handle mapDispatchToProps, I've tried a few with none working, here's what I have at the moment, getting no errors but still not functioning似乎有不同的方法来处理 mapDispatchToProps,我尝试了一些但没有工作,这就是我目前所拥有的,没有错误但仍然无法正常工作

the component:组件:

import React from 'react';
import { connect } from 'react-redux';
import { changeSelectedColor } from '../actions/actions';

const ColorPalette = ({ selectedColor, changeSelectedColor }) => {

    return(
        <div>
            <div className='flex justify-center mb-2'>
                <p>Selected color: {selectedColor}</p>
            </div>
            <div className='flex justify-center mb-2'>
                <button onClick={() => changeSelectedColor('test')} className='border border-black'>
                   click to change
                </button>
            </div>
        </div>
    )
}

const mapStateToProps = (state) => {
    return { 
        selectedColor: state.colorReducer.selectedColor, 
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        changeSelectedColor: (color) => dispatch(changeSelectedColor(color))    
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(ColorPalette);

the reducer:减速机:

import { combineReducers } from "redux";

const initialState = {
  selectedColor: 'black',
};

const colorReducer = (state = initialState, action) => {
    switch(action.payload) {
        case 'SET_SELECTED_COLOR':
            console.log('changing color to ', action)
            return {...state, selectedColor: action.payload};
        default:
            return state;
    }       
}

export default combineReducers({
  colorReducer,
});

the action:那个行动:

export const changeSelectedColor = (color) => {
    return {
        type: 'SET_SELECTED_COLOR',
        payload: color
    }
}

I've also tried passing mapDispatchToProps as an object, in which case every function is supposed to be automatically wrapped with dispatch if I understand correctly?我也尝试过将 mapDispatchToProps 作为 object 传递,在这种情况下,如果我理解正确,每个 function 都应该用调度自动包装? and as well passing no second argument to connect and having dispatch as a prop, dispatching the action directly on click, but like I said both methods failed并且没有传递第二个参数来连接并将调度作为道具,直接在点击时调度动作,但就像我说的两种方法都失败了

The issue comes from your reducer:问题来自您的减速器:

const colorReducer = (state = initialState, action) => {
    switch(action.type) { // <== here should be type not payload
        case 'SET_SELECTED_COLOR':
            console.log('changing color to ', action)
            return {...state, selectedColor: action.payload};
        default:
            return state;
    }       
}

Suggestion (not related to the answer)建议(与答案无关)

write your connect functions like below:编写您的连接函数,如下所示:

const mapStateToProps = ({ colorReducer: { selectedColor } = {} }) => ({ 
  selectedColor, 
});

const mapDispatchToProps = dispatch => ({
  changeSelectedColor: color => dispatch(changeSelectedColor(color)),
});

Also your action and reducer:还有你的动作和减速器:

export const changeSelectedColor = payload => ({
  type: 'SET_SELECTED_COLOR',
  payload,
});
const colorReducer = (state = initialState, { type, payload }) => {
  switch (type) {
    case 'SET_SELECTED_COLOR':
      return { ...state, selectedColor: payload };
    default:
      return state;
  }
};

Use restructuring assignment in ReactJS, it is so common and makes your codes more readable and better to debug.在 ReactJS 中使用重组分配,它很常见,使您的代码更具可读性和更好的调试性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM