-
Notifications
You must be signed in to change notification settings - Fork 535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onClick does not see state hooks changes #748
Comments
Could you explain what you mean by "the visualization"? It's possible that the onClick handler currently isn't change-able after mount, indeed. Don't think it has anything to do with hooks. |
@alex3165 Can we get this merged? |
I also noticed that the behavior of onClick is captured and never changed.
At the moment, a crappy way to work this around is to use a ref:
|
I dived into the code to understand why, because this was not the first time I faced this issue, and I think I found the mistake, but I will need @alex3165 to confirm this is a mistake. In the
It:
I think that this check |
@qlerebours You are correct, that's the problem. The solution is a little bit off, tho. The code right now checks:
But it's not checking if the listener simply changed. |
This is an issue for me, too, as it makes it difficult to use hooks and accommodate more complex interactions with maps. |
@ebrelsford I used the qlerebours solution, for me it worked well for now. |
I can also use the ref workaround but that doesn't seem ideal, right? |
Since, it's still not merged, I have // https://github.com/alex3165/react-mapbox-gl/issues/748#issuecomment-569395282
const _WORKAROUND_showList = React.useRef(true);
const [, forceUpdate] = React.useState();
// const [showList, setShowList] = React.useState(true);
// and
const handleMapClick = React.useCallback<MapEvent>(() => {
closePopup();
_WORKAROUND_showList.current = !_WORKAROUND_showList.current;
forceUpdate({});
// setShowList(!showList);
}, [
// setShowList,
// showList,
forceUpdate,
closePopup,
]); |
Any updates on this? right now im using the forked version of @sanfilippopablo |
Should be fixed in the latest version, thanks to the PR by @sanfilippopablo. If true, let's close this. |
@mklopets Looks good to me :) can you please also publish it to npm registry so I can use my default package.json? Thanks a lot! |
Sorry, thought I did 🤦 now it's properly published in |
I think these changes are not in the current npm release. Browsing the master branch commits, it seems like the PR changes were undone. Does anyone know why? |
Looks like the issue is still happening in the mapboxgl v4. Any work arounds to fix it? |
try #963 (comment) |
I am using react 16.8 hooks, in the map component I define an onClick function, but when I retrieve the value of the state hooks me the initial value is returned.
in the visualization it is possible to see the updated value of count, but within the function it is not possible
const [count,setCount] = useState(0); ... .... ... onClick={(map, evt) => { console.log(count); // Caso o indicador criando marcador esteja ativo ira criar marcadores no mapa. const { lng, lat } = evt.lngLat; setCriandoMarcador(true); if (criandoMarcador) { setPontosGps([...pontosGps, { lng, lat }]); if (!travarFerramenta) { setCriandoMarcador(false); } } }}
The text was updated successfully, but these errors were encountered: