0

Take this for example:

Assuming that the functions are all binded and everything

HalloweenCandyComponent.js

fetchHalloweenCandy() {
  this.props.getHalloweenCandy()
}

actions.js

getHalloweenCandy(){
  return (dispatch) => {  
    const request = axios.get('http://halloweencandy.com/all');
    request.then(function(response){
      console.log(response.data);
      return disatch({
        type: GET_ALL_HALLOWEEN_CANDY,
        payload: response.data
      })
    })
  }
}

At this point, when I console.log(response.data) I am returned with an array of objects as I should. Meaning, everything is working as expected.

Next, I want to update the HalloweenCandyComponent's state, and fill it with all the halloween candy. Hence, we are going to want to send this information to our reducers.

reducers/halloweencandy.js

export default function(state = [], action) {
    switch(action.type){
        case GET_ALL_HALLOWEEN_CANDY:
            console.log(action.payload);
            return [...state, 
                Object.assign({}, action.payload)
            ];

        default:
            return state;
    }
}

This is where I am getting tossed. console.log(action.payload) doesn't return anything. It will just be empty.

Another thing that I can't seem to figure out is... passing values to my actions. For example...

fetchHalloweenCandyByQuery(search){
  this.props.getHalloweenCandyByQuery(search);
}

Then in my actions... it always comes up as undefined... until many seconds later... (I tested that by doing a setTimeOut function...) But the point is I don't want to deal with that. I am using thunk, I am hoping that isn't the issue, but can't I have it so when the response is ready, it can execute, instead of giving me undefined values?! Kinda like a callback? Sorry if this is a stupid question, but I tried looking at the react-redux-async library and these but I am lost.

Community
  • 1
  • 1
dsomel21
  • 576
  • 2
  • 8
  • 27
  • Maybe `return this.props.getHalloweenCandyByQuery(search);` ? Also, you want to correct the typo in `dispatch`, as already mentioned by Geraint. – Michel Oct 31 '16 at 16:15
  • Hmmm... not quite... I added the `return` but it still comes up as undefined. Not exactly sure why this is :( – dsomel21 Oct 31 '16 at 16:41
  • You are trying to return from an asynchronous call? – epascarello Oct 31 '16 at 16:44
  • No, I am talking about the search query. When I load up the app, `search` comes up as undefined. That's what I can figure out... – dsomel21 Oct 31 '16 at 17:13

1 Answers1

0

You want to know what this issue was. response.data returns me an array of objects, hence, I can't be using Object.assign()

dsomel21
  • 576
  • 2
  • 8
  • 27