I am using axios on a React w/ Hooks front end to make a get request to populate my react-google-maps/api GoogleMaps Marker components using seed data in my rails backend. When I let the rails server run, the server repeatedly makes this call.
The following line causes the axios.get
to be called on a loop:
React.useEffect(() => {
// Get Coordinates from api
// Update Coordinates in state
axios.get('/api/v1/coordinates.json')
.then(response => response.data.data.map(coord =>
setCoordinateFromApi(coord.attributes)))
.catch(error => console.log(error))
}, [coordinates.length])
This successfully populates the map but means I can't use onClick's
functionality (because I presume the stack is being topped with this request?)
My index method on my CoordinatesController in Rails:
def index
coordinates = Coordinate.all
render json: CoordinateSerializer.new(coordinates).serialized_json
end
NB: this is my first project linking React to Rails as well as using Hooks
coordinates.length
from the dependency array inReact.useEffect
? Having more code would be great to have when investigating this issue. – Portwine