是什么导致Redux中更新状态出现“延迟”?

rlcwz9us  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(234)

我很难理解我在下面的Redux & React原生代码中做错了什么。
我正在尝试创建一个锻炼跟踪应用程序,用户可以在其中输入他们的锻炼进度。我创建了一个“workoutSessionSlice”来跟踪状态。

const initialState: IWorkoutSessionState = {
  currentTrackingEquipment: {
    createdOn: { seconds: 0, nanoseconds: 0 },
    equipmentName: '',
    repetitions: [],
    results: [],
    repetitionIndex: 0,
  },
  hasRunningSession: false,
  isTrackingEquipment: false,
  savedSessionEquipmentRecords: [],
};

export const workoutSessionSlice = createSlice({
  name: 'workoutSession',
  initialState,
  reducers: {
    saveEquipmentRecord: (
      state: any,
      action: PayloadAction<IEquipmentRecord>,
    ) => {
      state.savedSessionEquipmentRecords.push(action.payload);
    },
    addResult: (state: any, action: PayloadAction<IRepResult>) => {
      state.currentTrackingEquipment.results[
        state.currentTrackingEquipment.repetitionIndex
      ] = action.payload;
    },
    updateRepIndex: (state: any, action: PayloadAction<number>) => {
      state.currentTrackingEquipment.repetitionIndex = action.payload;
    },
  },
});

用户使用某个设备,并通过一个按钮将每次重复的结果添加到状态中。我注意到,当我使用console.log在此按钮的功能中记录有效负载和状态时,状态上存在一个“延迟”:

const handleAddRep = (success: boolean, nextRepIndex: boolean) => {

    console.log({
      repetitions:
        workOutSession.currentTrackingEquipment.results[
          workOutSession.currentTrackingEquipment.repetitionIndex
        ].repetitions,
      weight: equipmentTracking.wheelPickerValue,
      success,
    });

    dispatch(
      addResult({
        repetitions:
          workOutSession.currentTrackingEquipment.results[
            workOutSession.currentTrackingEquipment.repetitionIndex
          ].repetitions,
        weight: equipmentTracking.wheelPickerValue,
        success,
      }),
    );

    console.log(workOutSession);
  };

控制台:

Object {
  "repetitions": 15,
  "success": true,
  "weight": 100,
}

Object {
  "currentTrackingEquipment": Object {
    "createdOn": Object {
      "nanoseconds": 735000000,
      "seconds": 1671364848,
    },
    "equipmentName": "Bench-press-incline",
    "repetitionIndex": 0,
    "repetitions": Array [
      15,
      12,
      7,
    ],
    "results": Array [
      Object {                  // expected this object to be replaced
        "repetitions": 15,
        "success": false,
        "weight": 0,
      },
      Object {
        "repetitions": 12,
        "success": false,
        "weight": 0,
      },
      Object {
        "repetitions": 7,
        "success": false,
        "weight": 0,
      },
    ],
  },
  "hasRunningSession": true,
  "isTrackingEquipment": true,
  "savedSessionEquipmentRecords": Array [],
}

当我在按钮的功能之外手动记录状态时,我确实在数组&对象中看到了正确的数据:

Object {
  "currentTrackingEquipment": Object {
    "createdOn": Object {
      "nanoseconds": 735000000,
      "seconds": 1671364848,
    },
    "equipmentName": "Bench-press-incline",
    "repetitionIndex": 1,
    "repetitions": Array [
      15,
      12,
      7,
    ],
    "results": Array [
      Object {
        "repetitions": 15,
        "success": true,
        "weight": 100,
      },
      Object {
        "repetitions": 12,
        "success": false,
        "weight": 0,
      },
      Object {
        "repetitions": 7,
        "success": false,
        "weight": 0,
      },
    ],
  },
  "hasRunningSession": true,
  "isTrackingEquipment": true,
  "savedSessionEquipmentRecords": Array [],
}

问题是,当我想要保存记录时,这种“延迟”也会发生,导致结果数组中的最后一个对象没有更新。

onPress={() => {
  Alert.alert('Successfully finished rep?', undefined, [
    {
      text: 'Yes',
      onPress: () => {
        if (
          workOutSession.currentTrackingEquipment
            .repetitionIndex ===
          workOutSession.currentTrackingEquipment.results.length - 1
        ) {
          handleAddRep(true, false);  // Should update rep in "currentTrackingEquipment"
          Alert.alert('Finished tracking equipment?', undefined, [
            {
              text: 'Yes',
              onPress: () => {
                handleFinishEquipmentTracking(); // adds current tracking equipment to "savedSessionEquipmentRecords"
              },
            },
            {
              text: 'No, go back',
            },
          ]);
        } else {
          handleAddRep(true, true);
        }
      },
    },
  ]);
}}

我试过设置对象键的值而不是替换整个对象,但是效果不好。任何帮助都将不胜感激!
我试过设置对象键的值而不是替换整个对象,但是效果不好。任何帮助都将不胜感激!

gdx19jrr

gdx19jrr1#

没有“延迟”-您看到的是永远不会更新的旧(“过时”)数据。
请看这个例子:

let state = { foo: 1 }
const s = state
state = { foo: 2 }
// s is now { foo: 1 } and state is { foo: 2 }

仅仅因为你的状态更新为一个新值(而且它将是一个全新的对象,因为状态更新是不可变的)并不意味着你的局部变量会更新--它们仍然会指向之前的相同值。
在将来,该组件将重新呈现,然后const something = useSelector(...)将重新执行,然后你有了一个同名的新变量,这个新变量将有新的值(但很快又会运行stale)。

相关问题