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

Support React v16.3 context API #22

Closed
jaydenseric opened this issue Feb 28, 2018 · 9 comments
Closed

Support React v16.3 context API #22

jaydenseric opened this issue Feb 28, 2018 · 9 comments

Comments

@jaydenseric
Copy link

Function as children components are becoming very popular. In React v16.3 the new context API uses this pattern.

How do you walk into the children functions, so you can keep recursing the tree and visit deeper components?

For context, I am attempting to SSR Query components in graphql-react.

@jaydenseric
Copy link
Author

jaydenseric commented Mar 2, 2018

Ok, so it turns out my issue is not with function as children components.

With a simple test eventually foobar logs as the deepest node, as expected:

const TestComponent = ({ children }) => children('foobar')
const tree = <TestComponent>{message => <p>{message}</p>}</TestComponent>

reactTreeWalker(
  tree,
  element => {
    console.log(element)
    return true
  }
)

What does not work is Consumer from the new React context API:

const { Provider, Consumer } = React.createContext()
const tree = (
  <Provider value={{ message: 'foobar' }}>
    <Consumer>{({ message }) => <p>{message}</p>}</Consumer>
  </Provider>
)

reactTreeWalker(
  tree,
  element => {
    console.log(element)
    return true
  }
)

The deepest node is logged as [Function]. It seems the logic incorrectly thinks Consumer is a basic string or dom node element.

@jaydenseric jaydenseric changed the title How to walk into function as children components? Unable to traverse into React v16.3 context API Consumer components Mar 2, 2018
@ctrlplusb
Copy link
Owner

Looks like this is ready to be worked on 😀

https://github.com/apollographql/react-apollo/pull/1742/files#diff-f8938b597e7a7647b9ea5589d55d3a26

@jaydenseric are you happy with running with this?

@jaydenseric
Copy link
Author

@ctrlplusb thanks, but I've already worked it out in graphql-react: https://github.com/jaydenseric/graphql-react/blob/master/src/preload.mjs#L96

@ctrlplusb ctrlplusb changed the title Unable to traverse into React v16.3 context API Consumer components Support React v16.3 context API Mar 19, 2018
@oyeanuj
Copy link

oyeanuj commented Mar 30, 2018

@ctrlplusb Can I come back to the original question at the start of the thread by @jaydenseric:

How do you walk into the children functions, so you can keep recursing the tree and visit deeper components?

What is the expected behavior here?

@jaydenseric
Copy link
Author

@ctrlplusb relevent discussion: facebook/react#12501 (comment)

@lemonmade
Copy link

Has anyone taken this up? Would really like to convert a project using tree-walker to use new React context.

@ctrlplusb
Copy link
Owner

@lemonmade not as of yet. Would you be open to giving it a shot via a PR?

@dan-lee
Copy link
Contributor

dan-lee commented Jun 10, 2018

@ctrlplusb I tried something, maybe the PR can serve as a starting point?

@ctrlplusb
Copy link
Owner

Just merged @dan-lee's and published work on this. 🎉

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

No branches or pull requests

5 participants