iTwin / appui

Monorepo for iTwin.js AppUi
MIT License
8 stars 2 forks source link

`useActiveViewport` doesn't always update the returned `activeViewport` value #963

Closed grigasp closed 1 month ago

grigasp commented 1 month ago

Describe the bug

There's a race condition in useActiveViewport:

  1. As soon as the hook is called for the first time, it puts IModelApp.viewManager.selectedView to state. Let's say at that moment the viewport is undefined.
  2. At some point later, useEffect is called by React, where the hook subscribes to selected viewport changes.

It's possible, that the selected viewport is set in view manager between the steps 1 and 2. That causes the change event to be triggered, but the hook is not subscribed to it yet. In that case the activeViewport in state isn't updated.

To Reproduce

No response

Expected Behavior

I expect the hook to return selected viewport

Screenshots

No response

Desktop (please complete the applicable information)

No response

Additional context

No response

GerardasB commented 1 month ago

What AppUI version were you using? I think this might be resolved with 4.16.0

grigasp commented 1 month ago

What AppUI version were you using? I think this might be resolved with 4.16.0

It's not. I was able to set up a minimal example of the problem: https://codesandbox.io/s/blue-bush-qls2r2?file=/src/App.tsx. There you can see that Component2 doesn't get the counter value after it's updated by Component1.