Skip to content
Advertisement

React get value from a class

I have a component where i get the distance from one point to another, with Google Maps (DirectionsRenderer).

What i want, is to use this data in a h1

JavaScript

This is my return

JavaScript

EDIT:

ShowProduct.js (This is where i get the distance)

JavaScript

ProductCard.js (This is where want to use it)

JavaScript

Advertisement

Answer

You need to set up a state using useState hook, before your return:

JavaScript

Then in your onLoad callback, you need to set the actual value of the distance:

JavaScript

And finally in your h1 you can now use the value:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement