【发布时间】:2021-08-17 13:59:27
【问题描述】:
在同一个切片中,根据日志,有效地调度了状态,但是在记录选择器时,根状态不包含更新。它试图在文本框中写下整个句子。它已发送,但商店中没有任何内容,正如我使用 redux DevTools 检查时所确认的那样。
这是带有日志功能的切片:
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { NOTEBOOK } from '../../../constants/strings';
import type { RootState } from '../../../store';
import type { Notebook } from '../../../types/notebook';
import { InferenceField } from '../../../types/fields';
/** General type for notebooks archived or in editor */
const emptyNotebook: Notebook = {
id: null,
text1: null,
text2: null,
};
/** The slice of the store corresponding to the current data in notebook */
export const notebookSlice = createSlice({
name: NOTEBOOK,
initialState: emptyNotebook,
reducers: {
/** Merge update field(s) with the current notebook */
mergeInNotebook: (
state: Notebook,
action: PayloadAction<Partial<Notebook>>
) => {
state = { ...state, ...action.payload };
console.log('DEBUG > mergeInNotebook > state = ', state);
// Here, we see the state updated
},
/** replace the notebook by an empty notebook */
clearNotebook: (state: Notebook) => {
state = emptyNotebook;
},
},
});
/** Generate the actions creators for the notebook */
export const { mergeInNotebook, clearNotebook } =
notebookSlice.actions;
/** Selector for any field of the notebook */
export const selectNotebookInferenceFieldForDebug = (
state: RootState,
field: InferenceField
) => {
console.log('DEBUG > selectNotebookFieldForDebug > state = ', state);
// Here the state in unchanged
return state.notebook[field] || null;
};
// Export the reducer by default
export default notebookSlice.reducer;
如果不是来自切片,这里是商店:
import { combineReducers, configureStore } from '@reduxjs/toolkit';
import archiveReducer from '../modules/archive/archiveSlice';
import notebookReducer from '../modules/notebook/state/notebook';
import pipelineReducer from '../modules/inference/state/pipeline';
import predictionsReducer from '../modules/inference/state/predictions';
const reducer = combineReducers({
notebook: notebookReducer,
archive: archiveReducer,
pipeline: pipelineReducer,
predictions: predictionsReducer,
});
const store = configureStore({
reducer,
});
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type
export type DispatchType = typeof store.dispatch;
export default store;
以及应用根目录下store的调用
export default function App() {
const isLoadingComplete = useCachedResources();
const theme = useTheme();
if (!isLoadingComplete || !fontsLoaded) {
return null;
} else {
return (
<Provider store={store}>
<PaperProvider theme={theme}>
<SafeAreaProvider>
<Navigation theme={theme} />
<StatusBar />
</SafeAreaProvider>
</PaperProvider>
</Provider>
);
}
}
【问题讨论】:
标签: react-native redux react-redux redux-toolkit redux-store