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

[Bug]: useOutlet() does not return null anymore #8477

Closed
goffioul opened this issue Dec 13, 2021 · 1 comment · Fixed by #8483
Closed

[Bug]: useOutlet() does not return null anymore #8477

goffioul opened this issue Dec 13, 2021 · 1 comment · Fixed by #8483
Assignees
Labels

Comments

@goffioul
Copy link

What version of React Router are you using?

6.1.1

Steps to Reproduce

This is a follow-up of #8416. With 6.1.x, useOutlet() does not return null anymore, even when the nested route does not render. The OutletContext.Provider component is always rendered.

function Parent() {
    const outlet = useOutlet();

    return (
        <div className={outlet ? "has-children" : "">
            {outlet}
        </div>
    )
}

function Child() {
    ....
}

function App() {
    return (
        <Routes>
            <Route path="/my/url/path" element={<Parent />}>
                <Route path=":id" element={<Child />} />
            </Route>
        </Routes>
    );
}

Expected Behavior

useOutlet() returns null when the nested route does not render.

Actual Behavior

useOutlet() returns OutletContext.Provider element.

@goffioul goffioul added the bug label Dec 13, 2021
@superfaz
Copy link

superfaz commented Dec 13, 2021

Same issue.

The code for useOutlet() is:

export function useOutlet(context?: unknown): React.ReactElement | null {
  let outlet = React.useContext(RouteContext).outlet;
  return (
    <OutletContext.Provider value={context}>{outlet}</OutletContext.Provider>
  );
}

source:

export function useOutlet(context?: unknown): React.ReactElement | null {

I imagine that a test on the outlet value (being null or not) is missing.

The previous code (changed 4 days ago):

export function useOutlet(): React.ReactElement | null {
  return React.useContext(RouteContext).outlet;
}

Current workaround

Tests with outlet.props.children instead of outlet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants