简体   繁体   中英

React reducer that does not update the state

From a Redux tutorial I've been going through they allow you to add a place multiple times. I changed the reducer to reject duplicates. My question is, (see code), do I have to return the state if no updates are made or is there some other way of indicating no state is changed?

function placeReducer(state = initialState, action) {
  switch (action.type) {
    case ADD_PLACE:
        const existing = state.places.find((item) => item.value == action.payload);
        if (existing) {
            return {...state};
        }
        return {
            ...state,
            places: state.places.concat({
                key: Math.random(),
                value: action.payload
            })
        };
    default:
        return state;
  }
}

只需返回状态,无需创建新副本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM