Skip to content

Commit

Permalink
[Security Solution] Use a stable reference for tour context value (#1…
Browse files Browse the repository at this point in the history
…60487)

## Summary

The TourContext value was a const object declared in the render of a
component, which meant it was referentially different on every render,
this context provider is so high up in the component tree, this is a
problem and causing some re-renders. Many ways to fix this, but I opted
for something that kept the code as close to the existing code as I
could. A library like why-did-you-render will catch this, also eslint
will if you just try to log the value of context in a useEffect like
below:
<img width="1182" alt="image"
src="https://github.com/elastic/kibana/assets/56408403/64a5c8d3-24f9-4918-a9ba-fc36bbc0b671">
Nothing should change from a functionality/user perspective.
  • Loading branch information
kqualters-elastic authored Jun 27, 2023
1 parent 418fbb1 commit 96e6e22
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ export const RealTourContextProvider = ({ children }: { children: ReactChild })
setCompleteStep(tourId);
}, []);

const context = {
activeStep,
endTourStep,
incrementStep,
isTourShown,
setStep,
};
const context = useMemo(() => {
return {
activeStep,
endTourStep,
incrementStep,
isTourShown,
setStep,
};
}, [activeStep, endTourStep, incrementStep, isTourShown, setStep]);

return <TourContext.Provider value={context}>{children}</TourContext.Provider>;
};
Expand Down

0 comments on commit 96e6e22

Please sign in to comment.