简体   繁体   English

Redux-persist,类型错误:undefined is not a function

[英]Redux-persist, type error: undefined is not a function

This is my first implementation of redux, and redux-persist in react native.这是我第一次实现 redux,redux-persist in react native。 I got an error " type error: undefined is not a function", error comes when i tried map states.我收到错误“类型错误:未定义不是函数”,当我尝试 map 状态时出现错误。 Implementing redux works fine.实施 redux 工作正常。 When i console log my states i got my initial state.当我控制台记录我的状态时,我得到了我的初始 state。 I Tried to solve problem with documentation.我试图解决文档问题。 Unfortunately I haven't found the cause of the error.不幸的是,我还没有找到错误的原因。 Please kindly help me with this problem.请帮我解决这个问题。 All code is on: https://github.com/SusulAdam/Shopping-List_React-Native_Redux_TypeScript .所有代码都在: https://github.com/SusulAdam/Shopping-List_React-Native_Redux_TypeScript Below are some code snippets where most likely there is a bug以下是一些最有可能存在错误的代码片段

App.txs应用程序.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: 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: 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 };


Problem resolved, The problem was in the file shoppingRedux.ts.问题已解决,问题出在文件 shoppingRedux.ts 中。 Correct implementation part:正确的实现部分:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM