1

I am learning redux using react. I am trying to update an array of numbers based on a button click. I am specifically want to update the counter at specific index based on imported json file.

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { upVote, downVote } from '../store/actions/voteAction';

class Voter extends Component {
    render() {
        const { count, upVote, downVote, id} = this.props
        return (
            <div>
                <button onClick={() => upVote(id)}>+</button>
                The count is {count[id]}
                <button onClick={() => downVote(id)}>-</button>
            </div>
        )
    }
}

const mapDispatchToProps = dispatch => ({
    upVote: (payload) => dispatch(upVote(payload)),
    downVote: (payload) => dispatch(downVote(payload))
  });

const mapStateToProps = (state) => ({
    count: state.vote.count

})
export default connect(mapStateToProps, mapDispatchToProps)(Voter);

I think my issue comes with how i pass and update the payload in my reducer.

import {UP_VOTE,DOWN_VOTE} from '../actions/actionTypes'
import Mice from './../../imports/mice'
const initialState = {
    count: new Array(Mice.length).fill(0)
}


const voteReducer = (state=initialState, action) => {
    const id = action.payload
    switch(action.type){
        case UP_VOTE:
            return{
            ...state, count: state.count[id] + 1
            }
        case DOWN_VOTE:
            return{
            ...state, count: state.count[id] - 1
            }
        default:
            return state
    }

}

export default voteReducer;

I update the array, but every index is still changing and it appears i am still mutating the count array instead of an index inside it.

I have uploaded all my code to CodeSandbox for viewing and experimenting: CodeSandbox Link

Thanks for reading

3
  • i'm running your code, as I can see that you are not passing the proper id to upvote function from voter component. every time it's logging either 0 or 1 for id Commented Mar 10, 2019 at 2:36
  • I console.log(id) below const { count, upVote, downVote, id} = this.props in the voter component and i'm getting all 100+ indexes in my console. Can you be more specific? How are you getting the 1 or 0? thanks Commented Mar 10, 2019 at 2:58
  • ah, you got your answer :) Commented Mar 10, 2019 at 4:08

1 Answer 1

1

Use map method to create a new array, add change one element. The Redux switch will be:

switch (action.type) {
  case UP_VOTE:
    return {
      ...state,
      count: state.count.map((vote, i) => (i === id ? vote + 1 : vote))
    };
  case DOWN_VOTE:
    return {
      ...state,
      count: state.count.map((vote, i) => (i === id ? vote - 1 : vote))
    };
  default:
    return state;
}

Working code here https://codesandbox.io/s/74pmomo42j

Sign up to request clarification or add additional context in comments.

2 Comments

thank you man. I didn't even think of using the map function even though I used it in another component.
This is a typical way when you need to create a new state from an old one. And you can use the same to map objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.