What if I add multiple dependency in React's useEffect second parameter? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?ReactJS - Does render get called any time “setState” is called?Can you force a React component to rerender without calling setState?Infinite loop in useEffectHow to call loading function with React useEffect only onceUnexpected(?) behavior of reusable React hook functions or what am I doing wrong?Can I set state inside a useEffect hookIs there a way to loadFromLocalStorage and saveToLocalStorage in one useEffect hook?when using react useEffect my app is crashingWhat is the intension of using React's useCallback hook in place of useEffect?useGesture and useSpring keyboard support?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

How widely used is the term Treppenwitz? Is it something that most Germans know?

The logistics of corpse disposal

Is the Standard Deduction better than Itemized when both are the same amount?

Are my PIs rude or am I just being too sensitive?

Is a manifold-with-boundary with given interior and non-empty boundary essentially unique?

Bonus calculation: Am I making a mountain out of a molehill?

What is a Meta algorithm?

Sorting numerically

List *all* the tuples!

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

Why one of virtual NICs called bond0?

Stars Make Stars

Is there a "higher Segal conjecture"?

Right-skewed distribution with mean equals to mode?

What are 'alternative tunings' of a guitar and why would you use them? Doesn't it make it more difficult to play?

3 doors, three guards, one stone

Can a non-EU citizen traveling with me come with me through the EU passport line?

If a contract sometimes uses the wrong name, is it still valid?

How do I stop a creek from eroding my steep embankment?

Is it true that "carbohydrates are of no use for the basal metabolic need"?

Do I really need recursive chmod to restrict access to a folder?

ListPlot join points by nearest neighbor rather than order

Is there a service that would inform me whenever a new direct route is scheduled from a given airport?



What if I add multiple dependency in React's useEffect second parameter?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?ReactJS - Does render get called any time “setState” is called?Can you force a React component to rerender without calling setState?Infinite loop in useEffectHow to call loading function with React useEffect only onceUnexpected(?) behavior of reusable React hook functions or what am I doing wrong?Can I set state inside a useEffect hookIs there a way to loadFromLocalStorage and saveToLocalStorage in one useEffect hook?when using react useEffect my app is crashingWhat is the intension of using React's useCallback hook in place of useEffect?useGesture and useSpring keyboard support?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















For example if I make a useEffect hook as useEffect(() => ...,[a,b]). Will the useEffect get fired if one of [a,b] changes or when both [a,b] changes ?










share|improve this question

















  • 2





    no idea, so I filed github.com/facebook/react/issues/15068, which in the future I would highly recommend you also do. If the official documentation doesn't cover a question, you are almost guaranteed not to be the only person to wonder about whatever you're wondering about, and asking the people who make the thing you use to improve the docs is of great benefit to everyone.

    – Mike 'Pomax' Kamermans
    Mar 8 at 16:46











  • Or, if possible, just test it yourself : )

    – Powell_v2
    Mar 8 at 23:19











  • But when we test it, it may give an answer but you can't test all the edge cases and be sure about it. Why not just add a line in the documentation to remove all the doubts.

    – rockfight
    Mar 9 at 12:36

















1















For example if I make a useEffect hook as useEffect(() => ...,[a,b]). Will the useEffect get fired if one of [a,b] changes or when both [a,b] changes ?










share|improve this question

















  • 2





    no idea, so I filed github.com/facebook/react/issues/15068, which in the future I would highly recommend you also do. If the official documentation doesn't cover a question, you are almost guaranteed not to be the only person to wonder about whatever you're wondering about, and asking the people who make the thing you use to improve the docs is of great benefit to everyone.

    – Mike 'Pomax' Kamermans
    Mar 8 at 16:46











  • Or, if possible, just test it yourself : )

    – Powell_v2
    Mar 8 at 23:19











  • But when we test it, it may give an answer but you can't test all the edge cases and be sure about it. Why not just add a line in the documentation to remove all the doubts.

    – rockfight
    Mar 9 at 12:36













1












1








1








For example if I make a useEffect hook as useEffect(() => ...,[a,b]). Will the useEffect get fired if one of [a,b] changes or when both [a,b] changes ?










share|improve this question














For example if I make a useEffect hook as useEffect(() => ...,[a,b]). Will the useEffect get fired if one of [a,b] changes or when both [a,b] changes ?







reactjs react-hooks






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 16:27









rockfightrockfight

8491822




8491822







  • 2





    no idea, so I filed github.com/facebook/react/issues/15068, which in the future I would highly recommend you also do. If the official documentation doesn't cover a question, you are almost guaranteed not to be the only person to wonder about whatever you're wondering about, and asking the people who make the thing you use to improve the docs is of great benefit to everyone.

    – Mike 'Pomax' Kamermans
    Mar 8 at 16:46











  • Or, if possible, just test it yourself : )

    – Powell_v2
    Mar 8 at 23:19











  • But when we test it, it may give an answer but you can't test all the edge cases and be sure about it. Why not just add a line in the documentation to remove all the doubts.

    – rockfight
    Mar 9 at 12:36












  • 2





    no idea, so I filed github.com/facebook/react/issues/15068, which in the future I would highly recommend you also do. If the official documentation doesn't cover a question, you are almost guaranteed not to be the only person to wonder about whatever you're wondering about, and asking the people who make the thing you use to improve the docs is of great benefit to everyone.

    – Mike 'Pomax' Kamermans
    Mar 8 at 16:46











  • Or, if possible, just test it yourself : )

    – Powell_v2
    Mar 8 at 23:19











  • But when we test it, it may give an answer but you can't test all the edge cases and be sure about it. Why not just add a line in the documentation to remove all the doubts.

    – rockfight
    Mar 9 at 12:36







2




2





no idea, so I filed github.com/facebook/react/issues/15068, which in the future I would highly recommend you also do. If the official documentation doesn't cover a question, you are almost guaranteed not to be the only person to wonder about whatever you're wondering about, and asking the people who make the thing you use to improve the docs is of great benefit to everyone.

– Mike 'Pomax' Kamermans
Mar 8 at 16:46





no idea, so I filed github.com/facebook/react/issues/15068, which in the future I would highly recommend you also do. If the official documentation doesn't cover a question, you are almost guaranteed not to be the only person to wonder about whatever you're wondering about, and asking the people who make the thing you use to improve the docs is of great benefit to everyone.

– Mike 'Pomax' Kamermans
Mar 8 at 16:46













Or, if possible, just test it yourself : )

– Powell_v2
Mar 8 at 23:19





Or, if possible, just test it yourself : )

– Powell_v2
Mar 8 at 23:19













But when we test it, it may give an answer but you can't test all the edge cases and be sure about it. Why not just add a line in the documentation to remove all the doubts.

– rockfight
Mar 9 at 12:36





But when we test it, it may give an answer but you can't test all the edge cases and be sure about it. Why not just add a line in the documentation to remove all the doubts.

– rockfight
Mar 9 at 12:36












1 Answer
1






active

oldest

votes


















5














It will fire when either one changes. The way to think of it is that you are telling React:




a and b are the things that I am using inside this effect, so if either of them
change, my effect will need to cleanup the old version and re-execute with the updated values.




Here's a simple example that allows you to see the behavior:



import React, useState, useEffect from "react";
import ReactDOM from "react-dom";

function App()
const [dependency1, setDependency1] = useState(1);
const [dependency2, setDependency2] = useState(1);
useEffect(
() =>
console.log("only dependency1", dependency1, dependency2);
,
[dependency1]
);
useEffect(
() =>
console.log("dependency1 and dependency2", dependency1, dependency2);
,
[dependency1, dependency2]
);
return (
<div className="App">
<button
onClick=() =>
setDependency1(prev => prev + 1);

>
Change dependency1
</button>
<button
onClick=() =>
setDependency2(prev => prev + 1);

>
Change dependency2
</button>
<button
onClick=() =>
setDependency1(prev => prev + 1);
setDependency2(prev => prev + 1);

>
Change both
</button>
</div>
);


const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


Edit useEffect dependencies






share|improve this answer

























  • Looks like it gets fired when one of them changes. But why the documentation is not clear about it ? May be in future they will be clear about it. Nevertheless thank you for the clear answer.

    – rockfight
    Mar 9 at 12:34











  • It isn’t easy for the React team (who live and breathe this stuff) to predict what will and won’t be obvious to people. The documentation explains the purpose of the array (from which one could infer what the behavior should be). Describing all the edge cases in detail can make it harder to find the key concepts and make things appear more complicated than they are. If you think the documentation should be improved, submit a pull request with the changes that would have helped you understand this.

    – Ryan Cogswell
    Mar 9 at 13:20











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067202%2fwhat-if-i-add-multiple-dependency-in-reacts-useeffect-second-parameter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














It will fire when either one changes. The way to think of it is that you are telling React:




a and b are the things that I am using inside this effect, so if either of them
change, my effect will need to cleanup the old version and re-execute with the updated values.




Here's a simple example that allows you to see the behavior:



import React, useState, useEffect from "react";
import ReactDOM from "react-dom";

function App()
const [dependency1, setDependency1] = useState(1);
const [dependency2, setDependency2] = useState(1);
useEffect(
() =>
console.log("only dependency1", dependency1, dependency2);
,
[dependency1]
);
useEffect(
() =>
console.log("dependency1 and dependency2", dependency1, dependency2);
,
[dependency1, dependency2]
);
return (
<div className="App">
<button
onClick=() =>
setDependency1(prev => prev + 1);

>
Change dependency1
</button>
<button
onClick=() =>
setDependency2(prev => prev + 1);

>
Change dependency2
</button>
<button
onClick=() =>
setDependency1(prev => prev + 1);
setDependency2(prev => prev + 1);

>
Change both
</button>
</div>
);


const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


Edit useEffect dependencies






share|improve this answer

























  • Looks like it gets fired when one of them changes. But why the documentation is not clear about it ? May be in future they will be clear about it. Nevertheless thank you for the clear answer.

    – rockfight
    Mar 9 at 12:34











  • It isn’t easy for the React team (who live and breathe this stuff) to predict what will and won’t be obvious to people. The documentation explains the purpose of the array (from which one could infer what the behavior should be). Describing all the edge cases in detail can make it harder to find the key concepts and make things appear more complicated than they are. If you think the documentation should be improved, submit a pull request with the changes that would have helped you understand this.

    – Ryan Cogswell
    Mar 9 at 13:20















5














It will fire when either one changes. The way to think of it is that you are telling React:




a and b are the things that I am using inside this effect, so if either of them
change, my effect will need to cleanup the old version and re-execute with the updated values.




Here's a simple example that allows you to see the behavior:



import React, useState, useEffect from "react";
import ReactDOM from "react-dom";

function App()
const [dependency1, setDependency1] = useState(1);
const [dependency2, setDependency2] = useState(1);
useEffect(
() =>
console.log("only dependency1", dependency1, dependency2);
,
[dependency1]
);
useEffect(
() =>
console.log("dependency1 and dependency2", dependency1, dependency2);
,
[dependency1, dependency2]
);
return (
<div className="App">
<button
onClick=() =>
setDependency1(prev => prev + 1);

>
Change dependency1
</button>
<button
onClick=() =>
setDependency2(prev => prev + 1);

>
Change dependency2
</button>
<button
onClick=() =>
setDependency1(prev => prev + 1);
setDependency2(prev => prev + 1);

>
Change both
</button>
</div>
);


const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


Edit useEffect dependencies






share|improve this answer

























  • Looks like it gets fired when one of them changes. But why the documentation is not clear about it ? May be in future they will be clear about it. Nevertheless thank you for the clear answer.

    – rockfight
    Mar 9 at 12:34











  • It isn’t easy for the React team (who live and breathe this stuff) to predict what will and won’t be obvious to people. The documentation explains the purpose of the array (from which one could infer what the behavior should be). Describing all the edge cases in detail can make it harder to find the key concepts and make things appear more complicated than they are. If you think the documentation should be improved, submit a pull request with the changes that would have helped you understand this.

    – Ryan Cogswell
    Mar 9 at 13:20













5












5








5







It will fire when either one changes. The way to think of it is that you are telling React:




a and b are the things that I am using inside this effect, so if either of them
change, my effect will need to cleanup the old version and re-execute with the updated values.




Here's a simple example that allows you to see the behavior:



import React, useState, useEffect from "react";
import ReactDOM from "react-dom";

function App()
const [dependency1, setDependency1] = useState(1);
const [dependency2, setDependency2] = useState(1);
useEffect(
() =>
console.log("only dependency1", dependency1, dependency2);
,
[dependency1]
);
useEffect(
() =>
console.log("dependency1 and dependency2", dependency1, dependency2);
,
[dependency1, dependency2]
);
return (
<div className="App">
<button
onClick=() =>
setDependency1(prev => prev + 1);

>
Change dependency1
</button>
<button
onClick=() =>
setDependency2(prev => prev + 1);

>
Change dependency2
</button>
<button
onClick=() =>
setDependency1(prev => prev + 1);
setDependency2(prev => prev + 1);

>
Change both
</button>
</div>
);


const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


Edit useEffect dependencies






share|improve this answer















It will fire when either one changes. The way to think of it is that you are telling React:




a and b are the things that I am using inside this effect, so if either of them
change, my effect will need to cleanup the old version and re-execute with the updated values.




Here's a simple example that allows you to see the behavior:



import React, useState, useEffect from "react";
import ReactDOM from "react-dom";

function App()
const [dependency1, setDependency1] = useState(1);
const [dependency2, setDependency2] = useState(1);
useEffect(
() =>
console.log("only dependency1", dependency1, dependency2);
,
[dependency1]
);
useEffect(
() =>
console.log("dependency1 and dependency2", dependency1, dependency2);
,
[dependency1, dependency2]
);
return (
<div className="App">
<button
onClick=() =>
setDependency1(prev => prev + 1);

>
Change dependency1
</button>
<button
onClick=() =>
setDependency2(prev => prev + 1);

>
Change dependency2
</button>
<button
onClick=() =>
setDependency1(prev => prev + 1);
setDependency2(prev => prev + 1);

>
Change both
</button>
</div>
);


const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


Edit useEffect dependencies







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 17:06

























answered Mar 8 at 16:47









Ryan CogswellRyan Cogswell

6,0231626




6,0231626












  • Looks like it gets fired when one of them changes. But why the documentation is not clear about it ? May be in future they will be clear about it. Nevertheless thank you for the clear answer.

    – rockfight
    Mar 9 at 12:34











  • It isn’t easy for the React team (who live and breathe this stuff) to predict what will and won’t be obvious to people. The documentation explains the purpose of the array (from which one could infer what the behavior should be). Describing all the edge cases in detail can make it harder to find the key concepts and make things appear more complicated than they are. If you think the documentation should be improved, submit a pull request with the changes that would have helped you understand this.

    – Ryan Cogswell
    Mar 9 at 13:20

















  • Looks like it gets fired when one of them changes. But why the documentation is not clear about it ? May be in future they will be clear about it. Nevertheless thank you for the clear answer.

    – rockfight
    Mar 9 at 12:34











  • It isn’t easy for the React team (who live and breathe this stuff) to predict what will and won’t be obvious to people. The documentation explains the purpose of the array (from which one could infer what the behavior should be). Describing all the edge cases in detail can make it harder to find the key concepts and make things appear more complicated than they are. If you think the documentation should be improved, submit a pull request with the changes that would have helped you understand this.

    – Ryan Cogswell
    Mar 9 at 13:20
















Looks like it gets fired when one of them changes. But why the documentation is not clear about it ? May be in future they will be clear about it. Nevertheless thank you for the clear answer.

– rockfight
Mar 9 at 12:34





Looks like it gets fired when one of them changes. But why the documentation is not clear about it ? May be in future they will be clear about it. Nevertheless thank you for the clear answer.

– rockfight
Mar 9 at 12:34













It isn’t easy for the React team (who live and breathe this stuff) to predict what will and won’t be obvious to people. The documentation explains the purpose of the array (from which one could infer what the behavior should be). Describing all the edge cases in detail can make it harder to find the key concepts and make things appear more complicated than they are. If you think the documentation should be improved, submit a pull request with the changes that would have helped you understand this.

– Ryan Cogswell
Mar 9 at 13:20





It isn’t easy for the React team (who live and breathe this stuff) to predict what will and won’t be obvious to people. The documentation explains the purpose of the array (from which one could infer what the behavior should be). Describing all the edge cases in detail can make it harder to find the key concepts and make things appear more complicated than they are. If you think the documentation should be improved, submit a pull request with the changes that would have helped you understand this.

– Ryan Cogswell
Mar 9 at 13:20



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067202%2fwhat-if-i-add-multiple-dependency-in-reacts-useeffect-second-parameter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved