Skip to content
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

Connections: Avoid creating component during rendering & remove non-reusable hook #40347

Merged
merged 7 commits into from
Apr 11, 2024

Conversation

ravicious
Copy link
Member

This builds on top of #40249.

While trying to fix keyboard navigation (which I ultimately didn't do), I noticed that dynamically creating components like this completely messes up how React keeps track of element focus:

const sliderSteps = [
(props: StepComponentProps) => (
<Box p={2} ref={props.refCallback}>
<KeyboardArrowsNavigation>
<ConnectionsFilterableList
items={connections.items}
onActivateItem={activateItem}
onRemoveItem={connections.removeItem}
onDisconnectItem={connections.disconnectItem}
slideToVnet={props.next}
/>
</KeyboardArrowsNavigation>
</Box>
),
VnetSliderStep,
];
return (
<>
<ConnectionsIcon

This is because there's a new anonymous component created on every render. So then I refactored it into a proper standalone component. But then I ran into another problem: I had two different components calling useConnections, but that hook wasn't meant to be reusable.

In the end, I added some tests to capture the behavior of useConnections and then I got rid of the hook. I managed to replace useEffect with useState and updating a component during rendering. Check out the changes related to updateSorting.

Removing this hook is going to be useful since in the future I'll move closing/opening the connection list to a context so that I can control it outside of the component itself. This way when you want to start VNet we'll be able to automatically show the VNet panel.

This avoids creating a fresh component on each render of Connections.
Otherwise there are weird issues with focus and component identity.
useConnections calculates some stuff each time it's called, so it doesn't
make sense to call it from two different components. The next commit is
going to get rid of this hook anyway.
@ravicious ravicious added the no-changelog Indicates that a PR does not require a changelog entry label Apr 9, 2024
@ravicious ravicious requested review from avatus and gzdunek April 9, 2024 11:49
@ravicious
Copy link
Member Author

Hmm I messed something up an now I get a lot of act warning all out of sudden.

@ravicious
Copy link
Member Author

ravicious commented Apr 9, 2024

nvm, the act warnings are because of incorrectly resolved @testing-library/dom. #39883 (comment)

@ravicious ravicious added this pull request to the merge queue Apr 11, 2024
Merged via the queue into master with commit 735e03a Apr 11, 2024
40 checks passed
@ravicious ravicious deleted the r7s/remove-use-connections branch April 11, 2024 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog Indicates that a PR does not require a changelog entry size/md ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants