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, 2020 in Java-Script by kartik
• 37,520 points
961 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, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

0 votes
1 answer

How to trigger an ajax request when the user has finished typing in a text box?

Hello @kartik, Use the code below: //setup before functions var ...READ MORE

answered Sep 18, 2020 in Java-Script by Niroj
• 82,880 points
8,476 views
0 votes
1 answer

How to execute a function when page has fully loaded?

Hello @kartik, That's called load. It came waaaaay before ...READ MORE

answered Sep 23, 2020 in Java-Script by Niroj
• 82,880 points
1,280 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, 2020 in Java-Script by Niroj
• 82,880 points
995 views
+1 vote
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, 2020 in Java-Script by Niroj
• 82,880 points
25,681 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,938 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,959 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,894 views
+1 vote
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, 2020 in Laravel by Niroj
• 82,880 points
44,358 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, 2020 in Java-Script by Niroj
• 82,880 points
128,393 views
0 votes
1 answer

How to Change the selected value of a drop-down list with jQuery?

Hello @kartik, With hidden field you need to ...READ MORE

answered Sep 9, 2020 in Java-Script by Niroj
• 82,880 points
2,812 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP