I'm trying to map over an array of a fetched API to separate the individual items with the same name, and setState based on that individual item property. For instance, the array returns multiple different items with different latitudes and longitudes. I am able to setState using a function in the onClick inside the render function, however I know this is bad practice. I've tried moving the function outside of render and using onClick={this.functionName}, but this gives weird behavior. What am I doing wrong?
here is the function I created outside of the render:
mapItem = () => {
const parks = [...this.state.parks];
parks.map(item =>
this.setState({
lat: item.latitude,
lng: item.longitude,
zoom: 16
})
);
};
render() {
return (<h2 onClick={this.mapItem}>{item.name}</h2>)
}
this works:
render() {
const { parks } = this.state;
return(
{parks.map(item => (
<h2 onClick={() => this.setState({lat: item.latitude,
lng: item.longitude, zoom: 16})
}>{item.name}</h2>
))
)}}
thank you