Verified Commit 3f021561 authored by Florent Chehab's avatar Florent Chehab
Browse files

fix(hooks): more cleaning shared*

parent ce477414
......@@ -17,11 +17,8 @@ function useSharedReducer(key, reducer, initialState) {
setSharedState(state);
}, [state, setSharedState]);
const dispatchOut = useCallback(action => {
// built-in support for action that take dispatch as argument
if (typeof action === "function") {
action(dispatchOut);
} else {
const dispatchNotFunction = useCallback(
action => {
if (process.env.NODE_ENV !== "production") {
const { type } = action;
......@@ -40,8 +37,28 @@ function useSharedReducer(key, reducer, initialState) {
);
}
dispatch(action);
}
}, []);
},
[dispatch]
);
const dispatchFunction = useCallback(
action => {
action(dispatchNotFunction);
},
[dispatchNotFunction]
);
const dispatchOut = useCallback(
action => {
// built-in support for action that take dispatch as argument
if (typeof action === "function") {
dispatchFunction(action);
} else {
dispatchNotFunction(action);
}
},
[dispatchFunction, dispatchNotFunction]
);
// make sure to return the saved state to prevent bugs when directly accessing the saved data
return useMemo(() => [sharedState, dispatchOut], [sharedState, dispatchOut]);
......
......@@ -5,7 +5,7 @@ import useOnBeforeComponentMount from "./useOnBeforeComponentMount";
/**
* @type {Map<string, Set.<function>>}
*/
export const listenersByKey = new Map();
const listenersByKey = new Map();
/**
* Hook that provides a sharedState : when the setState is used in one component,
......
......@@ -51,8 +51,13 @@ function useSingleApiData(
const isLoading = !apiDataIsUsable(data);
const latestData = getLatestRead(data).data;
if (!data.isReading) {
// make sure performRead is triggered (it seems not te be triggered sometimes)
if (data.isInvalidated === true) performRead(params);
}
return { hasError, isLoading, latestData, data, setParams };
}, [data, setParams]);
}, [data, params, setParams]);
}
export default useSingleApiData;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment