浏览代码

fix reducer ref not working

AykutSarac 3 年之前
父节点
当前提交
20fe0bd6d0
共有 2 个文件被更改,包括 10 次插入12 次删除
  1. 9 11
      src/reducer/reducer.ts
  2. 1 1
      src/typings/global.ts

+ 9 - 11
src/reducer/reducer.ts

@@ -25,8 +25,6 @@ export const useConfigReducer: React.Reducer<AppConfig, ReducerAction> = (
   state = initialStates,
   action
 ) => {
-  if (!state.settings.zoomPanPinch) return state;
-
   switch (action.type) {
     case ConfigActionType.SET_CONFIG:
       return { ...state, settings: action.payload };
@@ -50,22 +48,22 @@ export const useConfigReducer: React.Reducer<AppConfig, ReducerAction> = (
       };
 
     case ConfigActionType.CENTER_VIEW:
-      state.settings.zoomPanPinch.resetTransform();
+      state.settings.zoomPanPinch?.resetTransform();
       return state;
 
     case ConfigActionType.ZOOM_IN:
-      state.settings.zoomPanPinch.setTransform(
-        state.settings.zoomPanPinch.state.positionX,
-        state.settings.zoomPanPinch.state.positionY,
-        state.settings.zoomPanPinch.state.scale + 0.2
+      state.settings.zoomPanPinch?.setTransform(
+        state.settings.zoomPanPinch?.state.positionX,
+        state.settings.zoomPanPinch?.state.positionY,
+        state.settings.zoomPanPinch?.state.scale + 0.2
       );
       return state;
 
     case ConfigActionType.ZOOM_OUT:
-      state.settings.zoomPanPinch.setTransform(
-        state.settings.zoomPanPinch.state.positionX,
-        state.settings.zoomPanPinch.state.positionY,
-        state.settings.zoomPanPinch.state.scale - 0.2
+      state.settings.zoomPanPinch?.setTransform(
+        state.settings.zoomPanPinch?.state.positionX,
+        state.settings.zoomPanPinch?.state.positionY,
+        state.settings.zoomPanPinch?.state.scale - 0.2
       );
       return state;
 

+ 1 - 1
src/typings/global.ts

@@ -7,5 +7,5 @@ export interface StorageConfig {
   autoformat: boolean;
   hideEditor: boolean;
   searchNode: string;
-  zoomPanPinch?: ReactZoomPanPinchRef;
+  zoomPanPinch: ReactZoomPanPinchRef | null;
 }