How to implement useEffect() so the list on the front page updates only when a name change has been detected?

0 votes

A page displays a list of objects [{name:, age:}, ...] A second page allows to update the name of a particular object. Then using hooks, how should I implement useEffect() so the list on the front page updates only when a name change has been detected?

const [objects, setObjects] = useState([]);

useEffect(()=> {
  getAllObjects()
},[getAllObjects, objects]);
Jun 1 in Java-Script by kartik
• 16,230 points
37 views

1 answer to this question.

0 votes

Hello @kartik,

Instead of passing the entire object to the dependency array, make sure that you only pass name which you can do by returning the names:

const [objects, setObjects] = useState([])

useEffect(()=> {
      getAllObjects()
}, [getAllObjects, ...objects.map(item => item.name)])

Thank You!!

answered Jun 1 by Niroj
• 33,570 points

Related Questions In Java-Script

0 votes
1 answer

How to delete a localStorage item when the browser window/tab is closed?

Hii @kartik, You can make use of the beforeunload event ...READ MORE

answered Jun 11 in Java-Script by Niroj
• 33,570 points
47 views
0 votes
1 answer

How to Scroll to the top of the page using JavaScript?

Hii @kartik, Using javascript <script> $("a[href='#top']").click(function() { ...READ MORE

answered Apr 2 in Java-Script by Niroj
• 33,570 points
21 views
0 votes
1 answer

How can we change the page URL without refreshing the page?

Hello @kartik,  In HTML5 you can change the ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 33,570 points
37 views
0 votes
1 answer

How to change url after success in ajax without page reload?

Hello @kartik, Use the browser history to change ...READ MORE

answered Apr 29 in Java-Script by Niroj
• 33,570 points
274 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 33,570 points
888 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 33,570 points
86 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 33,570 points
101 views
0 votes
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 33,570 points
512 views
0 votes
1 answer

How to find event listeners on a DOM node when debugging or from the JavaScript code?

Hii @kartik, It is possible to list all ...READ MORE

answered Jun 8 in Java-Script by Niroj
• 33,570 points
33 views
0 votes
1 answer

How to list the properties of a JavaScript object?

Hii @kartik, Use Reflect.ownKeys(): var obj = {a: 1, b: ...READ MORE

answered Jun 8 in Java-Script by Niroj
• 33,570 points
49 views