2
votes

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.

I am getting the prop from the initial state but unable to get the mapDispatchToProps to work properly. 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

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? 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

1

1 Answers

2
votes

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.