version X: Context HOC - Uncaught TypeError: n.children is not a function
See original GitHub issueI 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:
- Created 4 years ago
- Comments:10 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
These are not equivalent, in the former you are relying on the render function of Consumer to be this:
while in the latter you are relying on this implementation:
Because that is not a HOC, that’s a regular function for your case you’d need to do this:
You could also use the hooks API which allows you to just do this
const value = useContext(context)