我正在尝试将最近的搜索保存在本地存储中。
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中的搜索数组中,但它不保持以前的状态。仅显示当前用户输入。
答案 0 :(得分:1)
展开搜索时,您需要执行以下操作:
[ ...state.searches,
而不仅仅是:
[ ...state,
因为这会将state.venues (Array)
和state.searches (Array)
放入state.searches