【问题标题】:React Native with Redux toolkit > state dispatched but not updatedReact Native 与 Redux 工具包 > 状态已分派但未更新
【发布时间】: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


    【解决方案1】:

    这一行是问题:

    state = { ...state, ...action.payload };

    分配state = 不会做任何事情。 Immer 通过跟踪数据对象内嵌套字段的 mutations 或新引用的 returns 来工作。分配state = 只是将state 指向不同的值,既不是突变也不是返回语句。

    您应该改用return {...state, ... action.payload}Object.assign(state, action.payload)

    更多详情请查看https://redux-toolkit.js.org/usage/immer-reducers

    【讨论】:

    • 嗨,对!谢谢。我也觉得很奇怪。我不太担心,因为现在使用他们在教程“state.value -= 1”中提出的 React 工具包,很高兴看到它没有扩展到“状态”本身。
    • 是的,那是因为这是两个非常不同的语句:state.value = 改变了statevalue 字段inside,而state = 重新分配 state.
    • 谢谢,它澄清了。
    猜你喜欢
    • 2017-01-19
    • 2018-09-25
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多