Using document.querySelector in React? Should I use refs instead? How?

I can’t answer the “should you” part of whether to use refs for this instead other than if you do, you don’t need those id values unless you use them for something else. But here’s how you would: Use useRef(null) to create the ref. const activeSlideRef = useRef(null); Put it on the Slide that’s currently … Read more

Getting error after I put Async function in useEffect

You’re returning the result of calling getResponse() from the useEffect function. If you return anything from useEffect, it has to be a function. Changing your code to this should fix it because you’re no longer returning anything from the useEffect function. useEffect(() => { getResponse(); }); The useEffect Cleanup Function If you return anything from … Read more

What does it mean to ‘move this variable directly inside useEffect’ in this error message?

About useEffect hook: By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we’ll refer to it as our “effect”), and call it later after performing the DOM updates. more This means that function inside useEffect will be called after rendering of … Read more

Passing a function in the useEffect dependency array causes infinite loop

The issue is that upon each render cycle, markup is redefined. React uses shallow object comparison to determine if a value updated or not. Each render cycle markup has a different reference. You can use useCallback to memoize the function though so the reference is stable. Do you have the react hook rules enabled for … Read more

Why is the cleanup function from `useEffect` called on every render?

React performs the cleanup when the component unmounts. I’m not sure where you read this but this statement is incorrect. React performs the cleanup when the dependencies to that hook changes and the effect hook needs to run again with new values. This behaviour is intentional to maintain the reactivity of the view to changing … Read more

useEffect not called in React Native when back to screen

Below solution worked for me: import React, { useEffect } from “react”; import { useIsFocused } from “@react-navigation/native”; const ExampleScreen = (props) => { const isFocused = useIsFocused(); useEffect(() => { console.log(“called”); // Call only when screen open or when back on screen if(isFocused){ getInitialData(); } }, [props, isFocused]); const getInitialData = async () => … Read more

In useEffect, what’s the difference between providing no dependency array and an empty one?

It’s not quite the same. Giving it an empty array acts like componentDidMount as in, it only runs once. Giving it no second argument acts as both componentDidMount and componentDidUpdate, as in it runs first on mount and then on every re-render. Giving it an array as second argument with any value inside, eg , … Read more

In general is it better to use one or many useEffect hooks in a single component? [closed]

The pattern that you need to follow depends on your use case. First: You might have a situation where you need to add event listener during the initial mount and clean them up at unmount and another case where a particular listener needs to be cleaned up and re-added on a prop change. In such … Read more