D
D
dfv1232018-03-06 16:51:47
React
dfv123, 2018-03-06 16:51:47

Why asynchronous request fails?

1. I am learning to process asynchronous requests, I catch "items.map is not a function". What's the matter and how to fix it? My code:
https://codesandbox.io/s/vj2m955q37
2. In pursuit. How to make items received from the server not overwrite items from initialState, but join them?
3. When and what arrow function syntax to use?

export const fetchProductsBegin = () => ({
  type: FETCH_BEGIN
});

versus
export const fetchBegin = () => {
  return {
    type: "FETCH_BEGIN"
  };
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-03-06
@dfv123

1. Fix mapStateToProps :

const mapStateToProps = state => {
  return {
    items: state.items.items,
    error: state.items.error,
    loading: state.items.loading
  };
};

Write selectors in a good way.
2. Change the FETCH_SUCCESS case :
export function items(state = initialState, action) {
  const { type, payload } = action;

  switch (type) {

    // some code

    case "FETCH_SUCCESS":
      return { 
        ...state,
        items: [ ...state.items, ...payload.items ],
        loading: false,
      };

     // some code

  }
}

3. If possible, an abbreviated version:
export const fetchProductsBegin = () => ({ type: FETCH_BEGIN });

const mapStateToProps = state => ({
  items: state.items.items,
  error: state.items.error,
  loading: state.items.loading,
});

https://codesandbox.io/s/jprm2omlr5

R
Roman Aleksandrovich, 2018-03-06
@RomReed

https://codesandbox.io/s/73078n25qj

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question