Redux-persist, type error: undefined is not a function
Redux-persist, type error: undefined is not a function
这是我第一次实现redux,redux-persist在react native中。我收到错误“类型错误:未定义不是函数”,当我尝试映射状态时出现错误。
实施 redux 工作正常。当我控制台记录我的状态时,我得到了我的初始状态。
我试图解决文档问题。不幸的是我还没有找到错误的原因。
请帮我解决这个问题。所有代码均已开启:https://github.com/SusulAdam/Shopping-List_React-Native_Redux_TypeScript。
以下是一些很可能存在错误的代码片段
App.txs
import React, { FunctionComponent } from 'react';
import { Provider } from 'react-redux';
import { store, appPersist } from 'app/redux/shoppingRedux';
import { Navigation } from 'app/components/Navigation';
import { PersistGate } from 'redux-persist/integration/react';
const App: FunctionComponent = () => {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={appPersist}>
<Navigation />
</PersistGate>
</Provider>
);
};
export default App;
shoppingRedux.ts:
import { configureStore, createSlice, PayloadAction, getDefaultMiddleware } from '@reduxjs/toolkit';
import AsyncStorage from '@react-native-community/async-storage';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist';
import 'react-native-get-random-values';
import { nanoid } from 'nanoid';
export interface ShoppingListType {
id: string;
desc: string;
taken: boolean;
}
const shoppingListInitialState: ShoppingListType[] = [
{
id: nanoid(),
desc: 'milk',
taken: false,
},
{
id: nanoid(),
desc: 'bread',
taken: false,
},
];
const shoppingListSlice = createSlice({
name: 'shoppingList',
initialState: shoppingListInitialState,
reducers: {
create: {
reducer: (
state,
{ payload }: PayloadAction<{ id: string; desc: string; taken: boolean }>
) => {
state.push(payload);
},
prepare: ({ desc }: { desc: string }) => ({
payload: {
id: nanoid(),
desc,
taken: false,
},
}),
},
edit: (state, { payload }: PayloadAction<{ id: string; desc: string }>) => {
const editProduct = state.find((product) => product.id === payload.id);
if (editProduct) {
editProduct.desc = payload.desc;
}
},
taken: (state, { payload }: PayloadAction<{ taken: boolean; id: string }>) => {
const takenProduct = state.find((product) => product.id === payload.id);
if (takenProduct) {
takenProduct.taken = payload.taken;
}
},
delete: (state, { payload }: PayloadAction<{ id: string }>) => {
const idProduct = state.findIndex((product) => product.id === payload.id);
if (idProduct !== -1) {
state.splice(idProduct, 1);
}
},
},
});
export const {
create: createShoppingListCreator,
edit: editShoppingListCreator,
taken: takenShoppingListCreator,
delete: deleteShoppingListCreator,
} = shoppingListSlice.actions;
export const { select: selectdShoppingProductCreator } = selectedShoppingProductSlice.actions;
const persistConfig = {
key: 'root',
storage: AsyncStorage,
// whitelist: ['shoppingListSlice'],
};
const reducer = {
shoppingList: persistReducer(persistConfig, shoppingListSlice.reducer),
};
const middleware = [
...getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
];
export const store = configureStore({
reducer,
middleware,
});
export let appPersist = persistStore(store);
MainScreen.tsx:
import React, { useState } from 'react';
import { StyleSheet, View, Text, TextInput, Button } from 'react-native';
import { useSelector, useDispatch } from 'react-redux';
import {
ShoppingListType,
createShoppingListCreator,
editShoppingListCreator,
selectdShoppingProductCreator,
takenShoppingListCreator,
deleteShoppingListCreator,
} from 'app/redux/shoppingRedux';
interface ShoppingListState {
shoppingList: ShoppingListType[];
selectedProduct: string;
}
const MainScreen = () => {
const shoppingList = useSelector((state: ShoppingListState) => state.shoppingList);
return (
<>
<View
style={{
alignItems: 'center',
flex: 1,
backgroundColor: 'red',
}}
>
{shoppingList.map((shoppingElement: any) => (
<View style={{ flexDirection: 'row', padding: 10 }} key={shoppingElement.id}>
<Text
style={shoppingElement.taken && { textDecorationLine: 'line-through' }}
>
{shoppingElement.desc}
</Text>
</View>
))}
</View>
</>
);
};
export { MainScreen };
问题已解决,
问题出在文件 shoppingRedux.ts 中。
正确的实现部分:
const reducer = combineReducers({
shoppingList: shoppingListSlice.reducer,
selectedProduct: selectedShoppingProductSlice.reducer,
});
const middleware = [
...getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
];
const persistdReducer = persistReducer(persistConfig, reducer);
export const store = configureStore({
reducer: persistdReducer,
middleware: getDefaultMiddleware({
serializableCheck: false,
}),
});
export let appPersist = persistStore(store);
这是我第一次实现redux,redux-persist在react native中。我收到错误“类型错误:未定义不是函数”,当我尝试映射状态时出现错误。 实施 redux 工作正常。当我控制台记录我的状态时,我得到了我的初始状态。 我试图解决文档问题。不幸的是我还没有找到错误的原因。 请帮我解决这个问题。所有代码均已开启:https://github.com/SusulAdam/Shopping-List_React-Native_Redux_TypeScript。 以下是一些很可能存在错误的代码片段
App.txs
import React, { FunctionComponent } from 'react';
import { Provider } from 'react-redux';
import { store, appPersist } from 'app/redux/shoppingRedux';
import { Navigation } from 'app/components/Navigation';
import { PersistGate } from 'redux-persist/integration/react';
const App: FunctionComponent = () => {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={appPersist}>
<Navigation />
</PersistGate>
</Provider>
);
};
export default App;
shoppingRedux.ts:
import { configureStore, createSlice, PayloadAction, getDefaultMiddleware } from '@reduxjs/toolkit';
import AsyncStorage from '@react-native-community/async-storage';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist';
import 'react-native-get-random-values';
import { nanoid } from 'nanoid';
export interface ShoppingListType {
id: string;
desc: string;
taken: boolean;
}
const shoppingListInitialState: ShoppingListType[] = [
{
id: nanoid(),
desc: 'milk',
taken: false,
},
{
id: nanoid(),
desc: 'bread',
taken: false,
},
];
const shoppingListSlice = createSlice({
name: 'shoppingList',
initialState: shoppingListInitialState,
reducers: {
create: {
reducer: (
state,
{ payload }: PayloadAction<{ id: string; desc: string; taken: boolean }>
) => {
state.push(payload);
},
prepare: ({ desc }: { desc: string }) => ({
payload: {
id: nanoid(),
desc,
taken: false,
},
}),
},
edit: (state, { payload }: PayloadAction<{ id: string; desc: string }>) => {
const editProduct = state.find((product) => product.id === payload.id);
if (editProduct) {
editProduct.desc = payload.desc;
}
},
taken: (state, { payload }: PayloadAction<{ taken: boolean; id: string }>) => {
const takenProduct = state.find((product) => product.id === payload.id);
if (takenProduct) {
takenProduct.taken = payload.taken;
}
},
delete: (state, { payload }: PayloadAction<{ id: string }>) => {
const idProduct = state.findIndex((product) => product.id === payload.id);
if (idProduct !== -1) {
state.splice(idProduct, 1);
}
},
},
});
export const {
create: createShoppingListCreator,
edit: editShoppingListCreator,
taken: takenShoppingListCreator,
delete: deleteShoppingListCreator,
} = shoppingListSlice.actions;
export const { select: selectdShoppingProductCreator } = selectedShoppingProductSlice.actions;
const persistConfig = {
key: 'root',
storage: AsyncStorage,
// whitelist: ['shoppingListSlice'],
};
const reducer = {
shoppingList: persistReducer(persistConfig, shoppingListSlice.reducer),
};
const middleware = [
...getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
];
export const store = configureStore({
reducer,
middleware,
});
export let appPersist = persistStore(store);
MainScreen.tsx:
import React, { useState } from 'react';
import { StyleSheet, View, Text, TextInput, Button } from 'react-native';
import { useSelector, useDispatch } from 'react-redux';
import {
ShoppingListType,
createShoppingListCreator,
editShoppingListCreator,
selectdShoppingProductCreator,
takenShoppingListCreator,
deleteShoppingListCreator,
} from 'app/redux/shoppingRedux';
interface ShoppingListState {
shoppingList: ShoppingListType[];
selectedProduct: string;
}
const MainScreen = () => {
const shoppingList = useSelector((state: ShoppingListState) => state.shoppingList);
return (
<>
<View
style={{
alignItems: 'center',
flex: 1,
backgroundColor: 'red',
}}
>
{shoppingList.map((shoppingElement: any) => (
<View style={{ flexDirection: 'row', padding: 10 }} key={shoppingElement.id}>
<Text
style={shoppingElement.taken && { textDecorationLine: 'line-through' }}
>
{shoppingElement.desc}
</Text>
</View>
))}
</View>
</>
);
};
export { MainScreen };
问题已解决, 问题出在文件 shoppingRedux.ts 中。 正确的实现部分:
const reducer = combineReducers({
shoppingList: shoppingListSlice.reducer,
selectedProduct: selectedShoppingProductSlice.reducer,
});
const middleware = [
...getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
];
const persistdReducer = persistReducer(persistConfig, reducer);
export const store = configureStore({
reducer: persistdReducer,
middleware: getDefaultMiddleware({
serializableCheck: false,
}),
});
export let appPersist = persistStore(store);