React / Redux无法保持以前的搜索输入状态

时间:2018-04-24 00:37:40

标签: javascript reactjs redux

我正在尝试将最近的搜索保存在本地存储中。

Reducer.js

const initialState = {
  venues: [],
  searches: [],
};

const venueReducer = (state = initialState, action) => {
  switch (action.type) {
    case RECENT_SEARCHES:
      return {
        ...state,
        searches: [
          ...state,
          { near: action.payload.location, query: action.payload.place },
        ],
      };
  }
};

Action.js

export const fetchVenues = (place, location) => dispatch => {
  dispatch({ type: FETCH_VENUE_REQUESTED })
  dispatch({ type: RECENT_SEARCHES, payload: { place, location } })
};

现在我正在获取用户输入并将其保存到reducer中的搜索数组中,但它不保持以前的状态。仅显示当前用户输入。

1 个答案:

答案 0 :(得分:1)

展开搜索时,您需要执行以下操作:

[ ...state.searches,

而不仅仅是:

[ ...state,

因为这会将state.venues (Array)state.searches (Array)放入state.searches