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

Children is undefined even though route matches #646

Open
ryanmcclure4 opened this issue Jan 2, 2020 · 4 comments
Open

Children is undefined even though route matches #646

ryanmcclure4 opened this issue Jan 2, 2020 · 4 comments

Comments

@ryanmcclure4
Copy link

I'm experiencing an issue where I keep getting children = undefined in the parent's props when visiting a child route.

I'm using found with found-relay and I need to be able to visit routes matching /chat and /chat/12345. The following is part of my routing config:

...
{
  path: '/chat',
  Component: ChatView,
  query: graphql`...`,
  render: renderRoute,
  children: [
    { },
    {
      path: '/:threadId',
      Component: ThreadView,
      render: renderRoute
      query: graphql`...`,
    },
  ],
},
...

I am able to visit /chat just fine, and the page renders as expected. However, when I try to visit /chat/12345 I see the graphql request run, and the renderRoute function is called for the child route, however children is null for the parent ChatView component.

Now, if I modify this to use a named child as such:

...
{
  path: '/chat',
  Component: ChatView,
  query: graphql`...`,
  render: renderRoute,
  children: {
    thread: [
      {
        path: '/:threadId',
        Component: ThreadView,
        render: renderRoute
        query: graphql`...`,
      },
    },
  ],
},
...

Then I do see thread in the parent component's props, and child renders successfully.

What's going on here? Why doesn't the first example work?

@taion
Copy link
Contributor

taion commented Jan 3, 2020

Are you trying to access children inside ChatView or inside renderRoute?

children gets injected in a sort of janky way per

: React.cloneElement(element, { children });
by default, but this can fail for any number of reasons.

There's also support for the syntax in https://github.com/4Catalyzer/found#render where render can return a function that takes children, like:

render={({ Component }) => children => <Component>{children}</Component>}

But most likely it's something wonky there happening.

@ryanmcclure4
Copy link
Author

Yep, I'm trying to access children inside ChatView. Using render as a function that takes children seems to work as long as I pass it with a name other than children:

render = ({ Component }) => children => (
  <Component nestedRoute={children} >
    {children}
  </Component>
);

So it indeed appears as though something jank is happening. Even in the above case, nestedRoute does get passed through, but children is still coming into the parent component as undefined.

What's even more jank, is I have an almost identical case where nestedRoute never shows up in the parent component but children does! The only factor differentiating the two cases is that in the latter I have the component wrapped in connect() from react-redux, so this must be messing things up. Regardless, this only started happening after I recently upgraded all of my packages, so there may just be something else breaking it down the path.

@taion
Copy link
Contributor

taion commented Jan 3, 2020

Okay, that's bizarre. Is there any other sort of HoC that might be swallowing children?

@ryanmcclure4
Copy link
Author

They both use withRouter from found, as well as radium and a custom hoc using the Provider/Consumer api which passes along all props. However, I've tried removing all of these just to see if anything changes, and I still experience the same behavior.

I'll keep digging and try to figure out where things are getting messed up

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

No branches or pull requests

2 participants