Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing context from useEffect

I have a context that is used to show a full page spinner while my application is performing long running tasks.

When I attempt to access it inside useEffect I get a the react-hooks/exhaustive-deps ESLint message. For example the following code, although it works as expected, states that busyIndicator is a missing dependency:

const busyIndicator = useContext(GlobalBusyIndicatorContext);

useEffect(() => {
    busyIndicator.show('Please wait...');
}, []);

This article suggests that I could wrap the function with useCallback which might look as follows:

const busyIndicator = useContext(GlobalBusyIndicatorContext);
const showBusyIndicator = useCallback(() => busyIndicator.show('Please wait...'), []);

useEffect(() => {
    showBusyIndicator();
}, [showBusyIndicator]);

Although this works it has moved the issue to the useCallback line which now complains about the missing dependency.

Is it ok to ignore the ESLint message in this scenario or am I missing the something?

like image 213
Newm Avatar asked May 21 '19 14:05

Newm


People also ask

How to wrap the usecontext in the useref hook?

No need to Wrap your useContext in useRef Hook. you can update your context data just call in useEffect Brackets like this. const comingData = useContext (taskData); useEffect (() => { console.log ("Hi useEffect"); }}, [comingData]); //context data is updating here before render the component

What are the arguments of the useeffect function?

Here’s what finally made it click for me: the first two arguments of useEffect are a function ( didUpdate ), and an array of dependencies. the didUpdate function can return a function of its own, a cleanUp function. When a component mounts or the dependencies are updated, didUpdate is called.

What are the conditions for using useeffect?

In this case, “conditions” mean that one or more dependencies have changed since the last render cycle. Dependencies are array items provided as the optional second argument of the useEffect call. Array values must be from the component scope (i.e., props, state, context, or values derived from the aforementioned).

Can you use useeffect in a class component?

You can’t use useEffect (or any other hook) in a class component. Hooks are only available in functional components. If you want to refactor your lifecycle methods to use useEffect, you have to refactor entire class components writ large. This is both time-consuming and prone to error. What if you could refactor just this one part of the code?


1 Answers

If your busyIndicator is not changed during the life of the component, you could simply add it as a dependency to useEffect:

const busyIndicator = useContext(GlobalBusyIndicatorContext);

useEffect(() => {
    busyIndicator.show('Please wait...');
}, [busyIndicator]);

If busyIndicator could be changed and you don't want to see an error, then you could use useRef hook:

const busyIndicator = useRef(useContext(GlobalBusyIndicatorContext));

useEffect(() => {
    busyIndicator.current.show('Please wait...');
}, []);

The useRef() Hook isn’t just for DOM refs. The “ref” object is a generic container whose current property is mutable and can hold any value, similar to an instance property on a class. read more

like image 121
Andrii Golubenko Avatar answered Oct 11 '22 12:10

Andrii Golubenko