score:87
You are using it as a regular component, but it's actually a function that returns a component.
Try doing something like this:
const NewComponent = NewHOC(Movie)
And you will use it like this:
<NewComponent someProp="someValue" />
Here is a running example:
const NewHOC = (PassedComponent) => {
return class extends React.Component {
render() {
return (
<div>
<PassedComponent {...this.props} />
</div>
)
}
}
}
const Movie = ({name}) => <div>{name}</div>
const NewComponent = NewHOC(Movie);
function App() {
return (
<div>
<NewComponent name="Kill Bill" />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"/>
So basically NewHOC
is just a function that accepts a component and returns a new component that renders the component passed in. We usually use this pattern to enhance components and share logic or data.
You can read about HOCS in the docs and I also recommend reading about the difference between react elements and components
I wrote an article about the different ways and patterns of sharing logic in react.
score:0
In my case I forgot to remove this part '() =>'. Stupid ctrl+c+v mistake.
const Account = () => ({ name }) => {
So it should be like this:
const Account = ({ name }) => {
score:0
In my case
<Link key={uuid()} to="#" className="tag">
{post.department_name.toString}
</Link>
changed with
<Link key={uuid()} to="#" className="tag">
{post.department_name.toString()}
</Link>
score:1
In my case, I was transport class component from parent and use it inside as a prop var, using typescript and Formik, and run well like this:
Parent 1
import Parent2 from './../components/Parent2/parent2'
import Parent3 from './../components/Parent3/parent3'
export default class Parent1 extends React.Component {
render(){
<React.Fragment>
<Parent2 componentToFormik={Parent3} />
</React.Fragment>
}
}
Parent 2
export default class Parent2 extends React.Component{
render(){
const { componentToFormik } = this.props
return(
<Formik
render={(formikProps) => {
return(
<React.fragment>
{(new componentToFormik(formikProps)).render()}
</React.fragment>
)
}}
/>
)
}
}
score:1
it also happens when you call a function from jsx directly rather than in an event. like
it will show the error if you write like
<h1>{this.myFunc}<h2>
it will go if you write:
<h1 onClick={this.myFunc}>Hit Me</h1>
score:1
I was getting this from webpack lazy loading like this
import Loader from 'some-loader-component';
const WishlistPageComponent = loadable(() => import(/* webpackChunkName: 'WishlistPage' */'../components/WishlistView/WishlistPage'), {
fallback: Loader, // warning
});
render() {
return <WishlistPageComponent />;
}
// changed to this then it's suddenly fine
const WishlistPageComponent = loadable(() => import(/* webpackChunkName: 'WishlistPage' */'../components/WishlistView/WishlistPage'), {
fallback: '', // all good
});
score:1
What would be wrong with doing;
<div className="" key={index}>
{i.title}
</div>
[/*Use IIFE */]
{(function () {
if (child.children && child.children.length !== 0) {
let menu = createMenu(child.children);
console.log("nested menu", menu);
return menu;
}
})()}
score:2
I was able to resolve this by using my calling my high order component before exporting the class component. My problem was specifically using react-i18next
and its withTranslation method, but here was the solution:
export default withTranslation()(Header);
And then I was able to call the class Component as originally I had hoped:
<Header someProp={someValue} />
score:3
Adding to sagiv's answer, we should create the parent component in such a way that it can consist all children components rather than returning the child components in the way you were trying to return.
Try to intentiate the parent component and pass the props inside it so that all children can use it like below
const NewComponent = NewHOC(Movie);
Here NewHOC is the parent component and all its child are going to use movie as props.
But any way, you guyd6 have solved a problem for new react developers as this might be a problem that can come too and here is where they can find the solution for that.
score:4
I had this error too. The problem was how to call the function.
Wrong Code:
const Component = () => {
const id = ({match}) => <h2>Test1: {match.params.id}</h2>
return <h1>{id}</h1>;
};
Whereas id
is a function, So:
Correct code:
return <h1>{id()}</h1>;
score:5
I encountered this error while following the instructions here: https://reactjs.org/docs/add-react-to-a-website.html
Here is what I had:
ReactDOM.render(Header, headerContainer);
It should be:
ReactDOM.render(React.createElement(Header), headerContainer);
score:29
In my case i forgot to add the () after the function name inside the render function of a react component
public render() {
let ctrl = (
<>
<div className="aaa">
{this.renderView}
</div>
</>
);
return ctrl;
};
private renderView() : JSX.Element {
// some html
};
Changing the render method, as it states in the error message to
<div className="aaa">
{this.renderView()}
</div>
fixed the problem
score:61
I did encounter this error too because I didn't use the correct snytax at routing. This was in my App.js under the <Routes> section:
False:
<Route path="/movies/list" exact element={ MoviesList } />
Correct:
<Route path="/movies/list" exact element={ <MoviesList/> } />
So now the MoviesList is recognized as a component.
Source: stackoverflow.com
Related Query
- Functions are not valid as a React child. This may happen if you return a Component instead of from render
- React - Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render
- React - Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render
- Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render
- Functions are not valid as a React child. This may happen if you return a Component instead of <Component> from render
- Error : Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render
- React Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render
- Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. in react v6
- Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component />
- Warning: Functions are not valid as a React child.This may happen if you return a Component instead of <Component /> from render
- React is showing "Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render"
- React Auth0 (Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> )
- How to debug this error: Uncaught (in promise) Error: Objects are not valid as a React child
- React warning: Functions are not valid as a React child
- Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead
- React Router Dom v6 - Functions are not valid as a React child
- Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead
- react error: Functions are not valid as a React child
- Warning: Functions are not valid as a React child HOC
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {job}). If you meant to render a collection of children, use an array instead
- How to fix: Warning: Functions are not valid as a React child
- react-router-bootstrap - Warning: Functions are not valid as a React child
- Objects are not valid as a React child (found: object with keys). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: [object Element]). If you meant to render a collection of children, use an array instead.- ReactJs
- ReactJS axios call in useEffect shows - Warning: Functions are not valid as a React child
- Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead. ReactJS
- Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead
More Query from same tag
- Moment.js - I can't change the date to "pt-br" (ReactJS)
- how to change path with Link, NavLink, or Redirect (not full reload) in input navbar react
- Set react slick autoplayspeed dynamically without setState
- Can't access to variable inside a function arguments call
- React.js (Reference Error: React is not defined)
- How to align text input correctly in react native?
- 'flow property not found in props of react element' when is present for enzyme rendering of component
- React enzyme testing, Cannot read property 'have' of undefined
- React: Using Axios so I can use data from my first request in my second one
- Directing user to '/en/' or '/fr/' path upon loading page with React?
- Django react render
- How do I create `/orders/:id/detail` path in react-router
- How to copy nested object properties using spread operator
- Why I can not loop through object in react-typescript
- how can I get all_trades_total_profit in react
- How to set state in test when using enzyme and jest?
- Data manipulation (cleaning dataset before visualization) - Node.js
- How to make cards slide in React using buttons?
- Meteor method call with useTracker and render html
- Setting the font-family of the input field of a react-select
- Unable to fetch from firestore database
- Integrate React components into an existing NET Core app
- Can't get Webpack 2 HMR React to work
- How to pass api value to useState as its initial value?
- Continue fetching data until result is empty array
- How to update reducer with right state?
- React: Unable to access the parent component's function which is passed as prop to child component
- React: Inserting a line break in localised text
- Possibility to remove Parse.User.current() storing in localStorage
- App bar menu items not opening correct sub menu