How can I memoize my rawTranscript
variable so it doesn't trigger the useEffect
below which subsequently triggers the expensive transcriptParser
function? I've been trying a lot of different approaches, but the fact that I am using a redux-hook (useAppSelector
) to capture the data from the store means I cannot use an empty dependency useEffect for the initial mount (hooks can't be inside of useEffect). I also can't seem to wrap the useAppSelector
with a useMemo
either for the same reason.
Any thought's on how I can memoize the rawTranscript
variable so it doesn't re-trigger the useEffect
?
error when using the redux-hook inside useMemo
, useEffect
, useCallback
:
React Hook "useAppSelector" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.
component
const TranscriptCardController = (): JSX.Element => {
const dispatch = useAppDispatch();
// how to memoize rawTranscript?
const rawTranscript = useAppSelector(selectRawTranscript);
const parsedTranscript = useAppSelector(selectParsedTranscript);
useEffect(() => {
const parsedResult = transcriptParser(rawTranscript, undefined, 0.9);
dispatch(updateParsedTranscript(parsedResult));
}, [dispatch, rawTranscript]);
// ...
};
selector
export const selectRawTranscript = createSelector(
(state: RootState) => state.transcript.rawTranscript,
(rawTranscript): RawTranscript => rawTranscript
);