export interface Location{
latitude: number;
longitude: number;
}
const [routeLines, setRouteLines] = useState<Location []>([]);
When I use setRouteLines( routes => [...routes, location])
works, but I try this setRouteLines([...routeLines, location])
doesn't works. I don´t understand the difference.
CodePudding user response:
In your case, the answer might be because using the routeLines
for the previous value is not accurate.
React might do something called Batch when states change over time.
As a result, using the callback in setState
will guaruntee you are using the most recent state to make updates.