Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing Data Into My Redux State

Right now I am mapping over an array with an endpoint to my API. From there I am taking every link and calling a get request on each thing I map over. My issue is that I am not able to save everything into my redux state. I have tried using concat and push to take everything and put it all in one array in my redux state.

MomentContent.js:

componentDidMount () {

      this.props.photos.map(photo => {
        this.props.fetchPhoto(this.props.token, photo)}
      )
    }

index.js (actions):

export const fetchPhoto = (token, photo) => dispatch => {
  console.log('right token')
  console.log(token);
  fetch(photo, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': `Token ${token}`,
    }
  })
  .then(res => res.json())
  .then(parsedRes => {
    console.log('photo data')
    console.log(parsedRes)
    dispatch(getPhoto(parsedRes))
  })
}

export const getPhoto = (photo) => {
  console.log('RES')
  console.log(photo)
  return {
    type: GET_PHOTO,
    photo: photo
  }
}

When I use concat (reducer):

import {
  GET_PHOTO
} from '../actions';

const initialState = {
  photo: []
}

const photoReducer = (state = initialState, action) => {
  switch(action.type) {
    case GET_PHOTO:
      return {
        ...state,
        photo: initialState.photo.concat([action.photo])
      }

    default:
      return state;
  }
}

export default photoReducer

enter image description here

When I use push (reducer):

import {
  GET_PHOTO
} from '../actions';

const initialState = {
  photo: []
}

const photoReducer = (state = initialState, action) => {
  switch(action.type) {
    case GET_PHOTO:
      return {
        ...state,
        photo: initialState.photo.push([action.photo])
      }

    default:
      return state;
  }
}

export default photoReducer

enter image description here

UPDATE (another issue):

I was able to get it to work with :

return {
        ...state,
        photo: [...state.photo, action.photo]
      }

The issue now is that every time I refresh the same data is pushed again, so everything multiplies. Is there a way to fix this?

like image 337
Christian Lessard Avatar asked Aug 10 '18 19:08

Christian Lessard


People also ask

How do I add data to Redux?

Click the Add Data button and you can check that your store's state contains this large JSON data inside it. Thus you can add any amount of data in your app state by dispatching an action that stores that data in your Redux store through a reducer.

How do I get state data on Redux?

STEP-1 import useStore first from react-redux and then getState() function is used to access store state. STEP-2 area is the name of my slice in Redux store and areaName is state in that slice. STEP-3 FiletoSave variable is used to export JSON file with data from store.

Do I have to put all my state into Redux?

Based on those rules of thumb, most form state doesn't need to go into Redux, as it's probably not being shared between components. However, that decision is always going to be specific to you and your application.


2 Answers

You need to merge your updatedState and not initialState to the reducer in order to update

Either using concat:

return {
  ...state,
  photo: state.photo.concat([action.photo])
}

or using spread operator

return {
  ...state,
  photo: [...state.photo, action.photo]
}
like image 123
Pritish Vaidya Avatar answered Oct 10 '22 17:10

Pritish Vaidya


the push does not work correctly in redux, the ideal is to use the spread operator to concatenate the arrays

return {
     ... state,
     photo: [... initialState.photo, action.photo]
}
like image 27
Henrique Viana Avatar answered Oct 10 '22 18:10

Henrique Viana