刚开始在 Advanced Tutorial 之后的新 React Native 应用程序中集成 redux-toolkit,并立即遇到了 TypeError: (0 , _toolkit.combineReducers) is not a function
错误。接下来是我使用 redux-toolkit 的文件的简化结构:
src/store/UserReducer.ts
:
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
let initialState = {
isLoggedIn: false,
};
const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
setIsLoggedIn(state, action: PayloadAction<boolean>) {
state.isLoggedIn = action.payload;
},
},
});
export const { setIsLoggedIn } = userSlice.actions;
export default userSlice.reducer;
src/store/rootReducer.ts
:
import { combineReducers } from '@reduxjs/toolkit';
import UserReducer from './UserReducer';
const rootReducer = combineReducers({ user: UserReducer });
export default rootReducer;
src/store/index.ts
:
import { configureStore } from '@reduxjs/toolkit';
import rootReducer from './rootReducer';
const store = configureStore({ reducer: rootReducer });
export default store;
App.tsx
...
import React from 'react';
import { Provider } from 'react-redux';
import store from './src/store';
...
const App = () => {
return (
<Provider store={store}>
...
</Provider>
)
};
export default App;
"redux": "^4.0.5"
、"react-redux": "^7.2.2"
和 "@reduxjs/toolkit": "^1.5.0"
已安装。