Skip to content

Commit e69b468

Browse files
committed
move sync action into effect
1 parent 3574857 commit e69b468

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

packages/react/use-controllable-state/src/use-controllable-state-reducer.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ export function useControllableStateReducer<T, S extends {}, A extends AnyAction
107107
return internalState;
108108
}, [internalState, controlledState]);
109109

110-
// Sync internal state for controlled components so that reducer is called
111-
// with the correct state values
112-
if (isControlled && !Object.is(controlledState, internalState.state)) {
113-
dispatch({ type: SYNC_STATE, state: controlledState });
114-
}
110+
React.useEffect(() => {
111+
// Sync internal state for controlled components so that reducer is called
112+
// with the correct state values
113+
if (isControlled && !Object.is(controlledState, internalState.state)) {
114+
dispatch({ type: SYNC_STATE, state: controlledState });
115+
}
116+
}, [controlledState, internalState.state, isControlled]);
115117

116118
return [state, dispatch as React.Dispatch<A>];
117119
}

0 commit comments

Comments
 (0)