gpt4 book ai didi

javascript - React/Redux 无法保留搜索输入的先前状态

转载 作者:行者123 更新时间:2023-12-03 02:01:34 26 4
gpt4 key购买 nike

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

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中的搜索数组中,但它没有保留之前的状态。仅显示当前用户输入。

最佳答案

当您分散搜索时,您需要执行以下操作:

[ ...state.searches,

而不仅仅是:

[ ...state,

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

关于javascript - React/Redux 无法保留搜索输入的先前状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com