You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have this custom hook that utilizes dispatch and has it listed as a dependency. Doing this causes the hook to re-run repeatedly when used inside component.
Custom hook:
letuseFetchItbs=()=> {
letdispatch=Store.useDispatch();React.useCallback1(
()=> {
dispatch(SetFetchItbsStatus(Loading));Js.Promise.(
Itb.fetchItbs()->then_(
response => {
switch (response) {
|Ok(response) =>
dispatch(SetItbs(response.data));
dispatch(SetFetchItbsStatus(Success()));|Error(_error) =>
dispatch(
SetFetchItbsStatus(
Failure(
"There was a problem retriving ITBs at this time.",
),
),
)
};
resolve();
},
_,
)
->catch(
error => {
Js.log(error);
dispatch(
SetFetchItbsStatus(
Failure("There was a problem retriving ITBs at this time."),
),
);
resolve();
},
_,
)
)
->ignore;
},[|dispatch|],
);
};
Oh, yes, I looked at the implementation real quick and noticed that it is not memoizing dispatch, so every time you use useDispatch, a new function is created. I will prepare a fix for this 😄
I have this custom hook that utilizes dispatch and has it listed as a dependency. Doing this causes the hook to re-run repeatedly when used inside component.
Custom hook:
Hook being used:
Note that when I use
useCallback0
instead ofuseCallback1
and removedispatch
as a dependency of the hook, it only runs once as expected.The text was updated successfully, but these errors were encountered: