0

I am having a trouble while I was making simple weather app using react and redux.

I have two reducers. One is for adding data into an array and the another one is for removing a data from an array using find method.

here is the code :

// actions

// fetching weather api 

export const fetchWeather = term => {
  return async dispatch => {
    const res = await api.get(
      `/data/2.5/weather?q=${term}&APPID=132123123132`
    );
    dispatch({ type: "FETCH_WEATHER", payload: res.data });
  };
};

// delete a list that matches the selected id

export const listDelete = id => {
  return {
    type: "LIST_DELETE",
    paylod: id
  };
};

// reducers 

// reducer that put a new data into an array

const reducerIndex = (state = [], action) => {
  switch (action.type) {
    case "FETCH_WEATHER":
      return [...state, action.payload];
    default:
      return state;
  }
};

// reducer that deleting the data that id selected 

const reducerIndexDel = (state = [], action) => {
  switch (action.type) {
    case "LIST_DELETE":
      return state.find(city => city.id !== action.payload);
    default:
      return state;
  }
};

export default combineReducers({
  reducerIndex,
  reducerIndexDel
});

as you may know, browser gives me an error saying 'Given action "LIST_DELETE", reducer "reducerIndexDel" returned undefined.'

I am able to fetch the data that I want to have but How can I make reducerIndexDel can recognizes previous state?

1

2 Answers 2

1

I think you have written wrong spelling of - "payload" in listDelete function.

You are trying to access action.payload but it is written paylod there in action.

1
  • Exactly... I wasted few hours because of that small 'a'... and add some few changes to solve the problem. Thank you :)
    – GoonGamja
    Jan 28, 2019 at 9:12
1

Just use filter to get the state which do not contain the city with the id.

return state.filter(city => city.id !== action.payload)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.