Skip to content

SolaraViz: move hooks before early return in ComponentsView#2925

Merged
EwoutH merged 2 commits intomesa:mainfrom
EwoutH:viz_warning
Dec 23, 2025
Merged

SolaraViz: move hooks before early return in ComponentsView#2925
EwoutH merged 2 commits intomesa:mainfrom
EwoutH:viz_warning

Conversation

@EwoutH
Copy link
Copy Markdown
Member

@EwoutH EwoutH commented Dec 4, 2025

Problem

The ComponentsView component in solara_viz.py was violating Solara's Rules of Hooks (rule SH101: early return). The component had an early return statement when the components list was empty, but hook calls (use_state and use_effect) were placed after this conditional return. This caused the following warning during execution:

UserWarning: ComponentsView: `use_effect` found despite early return on line 369

This violation occurs because hooks must be called in the same order on every render to maintain proper state management. When components is empty, the function returns early and skips all hook calls. When components is not empty, the hooks are executed. This inconsistency can lead to state slots becoming misaligned between renders, potentially causing bugs and unexpected behavior.

Solution

This PR restructures the ComponentsView component to ensure all hooks (use_state and use_effect) are called at the top level before the early return statement. The empty components check has been moved inside the sync_layouts effect callback, allowing the hook to be called consistently on every render while still handling the empty case appropriately.

Testing

Warning is gone.

References

@EwoutH EwoutH requested a review from Sahil-Chhoker December 4, 2025 20:33
@EwoutH EwoutH added maintenance Release notes label visualisation labels Dec 4, 2025
@github-actions

This comment was marked as off-topic.

@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Dec 4, 2025

@Sahil-Chhoker, can you help out with this?

The warning seems to originate from ComponentsView, which has a use_effect hook called after an early return (line 419, after return on line 380). This violates React's Rules of Hooks - the hook is only called when components is not empty, causing inconsistent hook counts between renders.

A potential fix could be to move use_effect before the early return and handle the empty case inside the effect callback:

@solara.component
def ComponentsView(components, model):
    current_tab_index, set_current_tab_index = solara.use_state(0)
    layouts, set_layouts = solara.use_state({})
    
    def sync_layouts():
        if not components:
            return
        # ... build pages and sync logic here ...
    
    solara.use_effect(sync_layouts, [len(components)] if components else [])
    
    if not components:  # Now safe to return early
        return
    
    # ... rest of component ...

This would make sure all hooks are called in the same order on every render.

Does this have any negative effects? The early return was meant for something I can imagine.

@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Dec 17, 2025

@Sahil-Chhoker if you have the chance, could you take a look at this?

Copy link
Copy Markdown
Collaborator

@Sahil-Chhoker Sahil-Chhoker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this should cause any issues. I tested all the examples and didn’t encounter any problems, so it’s good to go.

@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Dec 19, 2025

Thanks for your comments. I don’t think the PR resolve this issue fully (see comment above), and I don’t knop what exactly is happening and what should be happening.

Moves solara.use_state() calls before the early return to comply with React's Rules of Hooks. Hooks must be called in the same order on every render and cannot be conditional.

Fixes Solara hooks validation warning (SH101).
Moves solara.use_state() calls before the early return to comply with React's Rules of Hooks. Hooks must be called in the same order on every render and cannot be conditional.

Fixes Solara hooks validation warning (SH101).
@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Dec 23, 2025

To a fresh stab at it and the warning is now gone. Merging.

@EwoutH EwoutH merged commit dc0adcf into mesa:main Dec 23, 2025
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Release notes label visualisation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants