How to stop calling the api again and again using react useEffect? - reactjs

I am using the redux toolkit, after adding the product dependency it is calling the API rapidly. How can I stop calling the API again and again?

There are two solutions:
Remove it as a dependency. if you only want dispatch when component first time render then use empty brackets []
Example:
useEffect(() => {
dispatch(fetchProducts());
}, []);
Use the useMemo hook, then the component will be rendered only the products have been changed.

Remove it as a dependency, if you only want to call it once when the component mounts just have an empty dependency. like this...
useEffect(() => {
dispatch(fetchProducts());
}, []);

remove dispatch in dependency:
useEffect(() => {
}, [products])

Related

How do you fix 'React Hook useEffect has a missing dependency:' warnings?

I get two warnings from these two useEffects. I am learning React.js and this is my first time dealing with this issue.
const fetchShippingCountries = async (checkoutTokenId) => {
const { countries } = await commerce.services.localeListShippingCountries(checkoutTokenId)
useEffect(() => {
fetchShippingCountries(checkoutToken.id);
}, []);
const fetchShippingOptions = async (checkoutTokenId, country, region = null) => {
const options = await commerce.checkout.getShippingOptions(checkoutTokenId, { country, region });
setShippingOptions(options);
setShippingOption(options[0].id);
}
useEffect(() => {
if(shippingSubdivision) fetchShippingOptions(checkoutToken.id, shippingCountry, shippingSubdivision);
}, [shippingSubdivision]);
React Hook useEffect has a missing dependency: 'checkoutToken.id'.
Either include it or remove the dependency array
React Hook useEffect has missing dependencies: 'checkoutToken.id' and 'shippingCountry'. Either include them or remove the dependency array
React is asking you to pass the 'checkoutToken.id' and 'shippingCountry' variables into the useEffect dependency array. useEffect is always called whenever the variables within dependency array change. Since you're using these two variables within useEffect, React is warning you that a change in them will not trigger the useEffect.
It's a part of the best practices to add all the variables being used by the useEffect function into the dependency array. This is to make sure you are keeping complete control over the changes within your React app.

React Useeffect running when page loads

Am using useEffect in a react functional component to fetch data from an external API but it keeps calling the API endpoint on render on the page .
Am looking for a way to stop the useeffect from running on render on the component
Use the dependency array (second argument to the useEffect), so you can specify when you need to run the useEffect.
The problem here is that you have not used the dependency array, so that it executes every time. By adding a dependency array, you specify the changes where you want useEffect to run.
useEffect(()=>{
},[<dependency array: which contains the properties>]);
If you leave the dependency array empty, it will run only once. Therefore if you want the API call to run only once, add an empty array as the second argument to your useEffect. This is your solution.
Like this:
useEffect(()=>{
//Your API Call
},[]);
useEffect is always meant to run after all the changes or render effects are update in the DOM. It will not run while or before the DOM is updated. You may not have given the second argument to useEffect, which if u do not provide will cause the useEffect to execute on each and every change. Assuming you only want to call the API just once when on after the first render you should provide an empty array.
Runs on all updates, see no second argument to useEffect:
useEffect(() => { /* call API */ });
Runs when the prop or state changes, see the second argument:
useEffect(() => { /* call API */ }, [prop, state]);
Runs only once, see the empty second argument:
useEffect(() => { /* call API */ }, []);
I recommend you to read the full documentation about the React useEffect hook.
Here is a easy example of using useEffect
function functionalComponent() {
const [data, setData] = React.useState(null);
React.useEffect(() => {
const url = 'https://randomuser.me/api/?results=10';
fetch(url)
.then(data => {
setData(data);
})
.catch(error => console.error(error))
}, []); // it's necessary to use [] to avoid the re-rendering
return <React.Fragment>
{data !== null && (
<React.Fragment>
{data.results.map(data => (
<div>
{data.gender}
</div>
))}
</React.Fragment>
)}
</React.Fragment>;
}
Maybe in your useEffect implementation you are avoiding the [] dependencies, this is a bit hard to understand if you come from class states. This on hooks review when a state element inside the hook change, for example if you are using an element that always change like a prop that you pass throught another component you might be setting inside the dependencies or another state, if you do not need any dependency just use it empty like the example above. As you can see in the documentation sometimes the dependencies are not used, this might generate an infinite loop.

Missing dependency: 'dispatch' in React

In my react/redux app, i'm using dispatch to call the action that retrieve data from state in redux each time the component is mounted. The problem is happening on useState My way does not work
Below is the error I'm getting:
React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array. Outer scope values like 'getInvoiceData' aren't valid dependencies because mutating them doesn't re-render the component react-hooks/exhaustive-deps
Here is my code:
const TableSection = () => {
const invoiceData = useSelector((state => state.tables.invoiceData));
const dispatch = useDispatch()
useEffect(() => {
dispatch(getInvoiceData());
}, [getInvoiceData]);
(...)
export default TableSection;
You need to add dispatch function to dep array:
const TableSection = () => {
const dispatch = useDispatch()
useEffect(() => {
dispatch(getInvoiceData());
}, [dispatch]);
Its safe to add it to dep array because its identity is stable across renders, see docs.
Note: like in React's useReducer, the returned dispatch function identity is stable and won't change on re-renders (unless you change the store being passed to the , which would be extremely unusual).
This is not an error, its just a warning.
You can fix this by adding dispatch in the dependency array.
useEffect(() => {
dispatch(getInvoiceData());
}, [dispatch]);
second part of the warning message states, Outer scope values like 'getInvoiceData' aren't valid dependencies because mutating them doesn't re-render the component react-hooks/exhaustive-deps, you also need to remove getInvoiceData function from the dependency array of useEffect hook.
Anything from the scope of the functional component, that participates in react's data flow, that you use inside the callback function of useEffect should be added in the dependency array of the useEffect hook.
Although, in your case, it is safe to omit dispatch function from the dependency array because its guaranteed to never change but still it won't do any harm if you add it as a dependency.

replacing componentDidMount with useEffect

I was going through React Hooks docs and it mentioned
If you’re familiar with React class lifecycle methods, you can think
of useEffect Hook as componentDidMount, componentDidUpdate, and
componentWillUnmount combined.
Suppose I have a class component right now where in componentDidMount I am doing something like this
componentDidMount() {
MapboxGL.setTelemetryEnabled(false);
}
As far as I can recall, Component did mount is only called once in lifecycle?
If I were to use react hooks then it would be something like this
useEffect(() => {
MapboxGL.setTelemetryEnabled(false);
});
This would call my function everytime state changes in react functional hooks component? Wouldn't it be redundant to call MapboxGL.setTelemetryEnabled(false); to call this everytime? when you only want to do it once component have mounted?
React docs have showed how useEffect can replace multiple lifecycle methods but I am still unable to comprehend how react hooks can replace componentDidMount?
Also, Just a side note question, Can you make a full fledge app using hooks (something like foursquare or instagram?)
You need to add a dependency array for it to know when to recall this hook. An empty dep array will only call it once aka "on mount". And if you don't provide a dep array then it will just be called on every re-render.
useEffect(() => {
MapboxGL.setTelemetryEnabled(false);
}, []);
You can create a flag and if the flag is false/true then only perform that action. something as simple as this
useEffect(() => {
if (something) {
MapboxGL.setTelemetryEnabled(false);
setSomething(false)
}
});
or if you only need hook once, you can do what Matt have suggested
useEffect(() => {
MapboxGL.setTelemetryEnabled(false);
}, []);

React useEffect() Infinite loop with firebase and react-router

I've seen some answers say that to run useEffect only once, you have to give an empty list as second argument. This is my code
const history = useHistory();
useEffect(() => {
console.log('hi')
firebase.auth().onAuthStateChanged((user) => {
if (user)
history.push('/')
})
},[])
First the linter tells me that 'React Hook useEffect has a missing dependency: 'history'. Either include it or remove the dependency array'. I don't understand this because history is already removed since second argument is an empty array.
Second console logs hi infinitely. Why?
You cannot run a use effect hook only once if it uses a dependancy. Every time the dependancy changes it will force the hook to re-run.
You have to add history to the dependancy list because you are using the push method on history inside useEffect. This is non-negotiable.
useEffect(() => {
console.log('hi')
firebase.auth().onAuthStateChanged((user) => {
if (user)
history.push('/')
})
},[history])
You will have to switch back to classes and use the componentDidMount method if you want to fire a function with dependency only once.

Resources