mmomtchev/rlayers

Calling an event only once

andreighh opened this issue · 3 comments

Hello,

Is it possible to call an event only once for the map (map.once("rendercomplete"...)? I'm trying to use onRenderComplete but I want it to run only once. Thank you.

You should do it the React way - by adding state to your component and setting that handler based on the state.

This is the React way of doing things.

First:

const [called, setCalled] = React.useState(false);

Then:

<.... onRenderComplete={called ? undefined : yourHandler} ... >

And in the handler:
setCalled(true)

I was thinking it's not the right way, but I see I'm wrong, thank you for the help and your time.