question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

version X: Context HOC - Uncaught TypeError: n.children is not a function

See original GitHub issue

I am trying to create a HOC using Context and I am getting Uncaught TypeError: n.children is not a function

Here is the basic code

index.js:

import { render } from 'preact';
import MyProvider from 'context';
import Button from 'button';

render(
     <MyProvider color="red">
         <Button/>
    </MyProvider>,
    target
);

context.js

import { Component, createContext } from 'preact';

const context = createContext();

export default class MyProvider extends Component {
	render() {
		const { children, ...value } = this.props;
		return (
			<context.Provider value={value}>{children}</context.Provider>
		);
	}
}

// this is the part that is faling
export const withContext = (WrappedComponent) => {
    return class ContextConsumer extends Component {
      render() {
        return (<context.Consumer><WrappedComponent /></context.Consumer>);
      }
    }
}

button.js

import { Component } from 'preact';
import { withContext } from 'context';

class Button extends Component {
    render() {
        return (
            <button>test</button>
        );
    }
}

export default withContext(Buttonn);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
JoviDeCroockcommented, Jul 25, 2019

These are not equivalent, in the former you are relying on the render function of Consumer to be this:

render() {
  return this.props.render(context)
} 

while in the latter you are relying on this implementation:

render() {
  return this.props.children(context)
} 
1reaction
JoviDeCroockcommented, Jul 25, 2019

Because that is not a HOC, that’s a regular function for your case you’d need to do this:

export const wrapWithContext = (WrappedComponent) => {
  return (props) => <context.Consumer>{(ctx) => <WrappedComponent ctx={ctx} {...props} />}</context.Consumer>
}

You could also use the hooks API which allows you to just do this const value = useContext(context)

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: _nextProps.children is not a function - Stack Overflow
I'm trying to use react context with nextJS and i'm getting this error: Server Error TypeError: _nextProps.children is not a function.
Read more >
a context consumer was rendered with multiple children, or a ...
Basically, we are saying that if children prop is function and currentElement is just function too (not ClassComponent), we are gonna pass those...
Read more >
Understanding the "Objects are not valid as a react child" Error ...
The error "Objects are not valid as a React child" is rather common and the solution is to manipulate the object so that...
Read more >
Context - React
Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a...
Read more >
Access the navigation prop from any component
An ordinary component that is not a screen component will not receive the navigation prop automatically. For example in this GoToButton component:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found