gpt4 book ai didi

javascript - 如何不在React Native中将搜索结果保存在redux-persist中?

转载 作者:行者123 更新时间:2023-12-02 21:24:11 24 4
gpt4 key购买 nike

我有顶级选项卡,用于呈现搜索请求的结果数据,

所以我找不到任何将数据传递到选项卡的解决方案!

因此,我在从 API 获取响应后使用 redux 保存结果数据,并将其作为选项卡屏幕中的 prop 传递。

但是我在使用 redux-persist 在本地保存状态时遇到了一些问题,

但在这种情况下,我不想将这些数据保存在存储中,那么有没有办法处理它“就在这种情况下(搜索结果)”?

代码

搜索.js

 onSearch = async () => {
const {searchText} = this.state;

let response = await API.post('/search', {
name: searchText,
});
let {
data: {
data: {data},
},
} = response;

this.props.dispatch(getResult(data))

}

歌曲标签

{props.artists.length <= 0 ? (
<Text style={[styles.serachFindText, {paddingVertical: 15}]}>
Not found ☹️
</Text>
) : (
... Flatlist ...
)}

错误

Cannot read property 'length' of undefined

行动

import {GET_SEARCH_RESULT} from './types';
export const getResult = (songs, artists) => {
return {
type: GET_SEARCH_RESULT,
songs,
artists,
// payload: result,
};
};

reducer

import {GET_SEARCH_RESULT} from '../actions/types';

let initial_state = {
searchResult: [], // maybe it's wrong?
};
const searchResultReducer = (state = initial_state, action) => {
const {payload, type} = action;
switch (type) {
case GET_SEARCH_RESULT:
return {...state, artists: action.artists, songs: action.songs};
default:
return state;
}
};

export default searchResultReducer;

err

商店

..
import searchResultReducer from '../reducers/searchResultReducer';
..


const persistConfig = {
key: 'root',
storage: AsyncStorage,
};


const rootReducer = combineReducers({
...
searchResult: searchResultReducer,
...
});

const persistedReducer = persistReducer(persistConfig, rootReducer);



export const store = createStore(
persistedReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
);
export const persistor = persistStore(store);

最佳答案

是的,您可以将 blacklist 键添加到 persistConfig 中,并使用包含您不想保留的键的数组。对于您的情况:

const persistConfig = {
key: 'root',
storage: AsyncStorage,
blacklist: ['searchResult'],
};

Check all config options for the persistReducer here .

关于javascript - 如何不在React Native中将搜索结果保存在redux-persist中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60779496/

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