I'm learning react native. I didn't figure out this problem. I have been using functional component in my project. Here is i don't understand to convert class component state :
constructor(props) {
super(props)
this.state = {
center: { lat: 5.6219868, lng: -0.23223 },
locations: {},
users_online: [],
current_user: ''
}
}
/* this.setState((prevState, props) => {
let newState = { ...prevState };
newState.center = location;
newState.locations[`${prevState.current_user}`] = location;
return newState;
}); */ ==> I wan't to convert to functional component this state.
Thank you for answering.!
This is kind of a broad question and I think you should follow any getting started with hooks tutorial to understand what's going on. Anyways, in your case, you will first need to change it into a function instead of a class, then specify the four states values you have using an useState for each. For the batched updates, wrap the state settings functions inside an unstable_batchedUpdates method to trigger a single rerender for multiple state updates. Keep in mind, that the last method is unstable and might change in the future, but that gets things done for now.