Hi folks! Question on reacting to data changes without refreshing the entire cell.
In my case, I have a map, and on that map I have a marker. If I want the marker’s position to be updated with the value of another cell, the entire map redraws when the value changes.
To invert a recent meme: well, no but kind of yes.
Basically: no, you cannot partially re-evaluate a cell. Therefore, the solution is to split out the cell into more fine-grained little cells, so that the parts of the cell that you don’t want to re-evaluate don’t do so. It’s basically just good old refactoring, but with Observable cells!
In your example, the cell in question
creates a mapbox object
creates a marker object and adds it to the mapbox object
sets the marker object to the value of the longitude cell
What you basically are saying is that you only want to re-evaluate the last step when the longitude cell updates. So we need to split this cell into three cells: one for each described step. Then only the last cell will depend on longitude, meaning the other two won’t re-evaluate. Which was essentially what you were going for, right?
marker = {
const marker = new mapboxgl.Marker()
.setLngLat([0, 0])
.addTo(map);
// make sure we clean up the marker if we re-run this cell too!
invalidation.then(() => marker.remove());
return marker;
}
// make only this call depend on changes to `longitude` cell
marker.setLngLat([longitude, 0])