I'm trying to update the react component's local state when an element is clicked
onChildClick={this.onChildClickCallback}
where onChildClickCallback
is a helper function outside the render which is as follows:
onChildClickCallback = key => {
this.setState({ items: this.props.data, newKey: key }, function() {
console.log(this.state);
});
};
I've written the above console.log in callback as I've read somewhere state might not get updated immediately.
But, whatever the value I pass to the state, it is not getting updated and console is logging the same old value of key and items. What am I missing here?
UPDATED CODE:
render() {
console.log(this.state);
const items = this.props.data;
return (
<Fragment>
<GoogleMap
center={this.state.center}
onChildClick={this.onChildClickCallback}
>
{items.map(item => (
<Marker
key={item.id}
lat={item.lattitude}
lng={item.longitude}
place={place}
/>
))}
</GoogleMap>
</Fragment>
);
}
And you have onChildClickCallback already. I wanted to update the state and rerender the component when onChildClickCallback is triggered.