React Function Higher Order Component

Raja Kondla

Higher Order Component not working for functional component.

I am trying to create a sample functional HOC. I am using react hook useState and passing the state to the passed component. Below is my code

function HOC(WrapperComponent) {
  // outside of return function
  const [course] = useState({ id: 0, name: "Raja/Kondla" });
  return function() {
     return <WrapperComponent course={course} />;
  };
}

const Comp = HOC(({ course }) => {
  return (
    <>
      <div>HOC</div>
      <div>{course.id}</div>
      <div>{course.name}</div>
      <div>-------------------</div>
    </>
  );
});

ReactDOM.render(<Comp />, rootElement);

When I run it I am getting below error.

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app

but if I put below code as HOC working fine.

function HOC(WrapperComponent) {
  return function() {
      // inside return function
    const [course] = useState({ id: 0, name: "Raja/Kondla" });
    return <WrapperComponent course={course} />;
  };
}

getting the result output:

HOC
0
Raja/Kondla

Can someone explain me what is the reason? and using functional HOC, will there be any performance impact?

Thanks in advance

Sagiv b.g

Your HOC is a function which returns a react component this is why it works when you put the line inside the returned function component.

With that said, I would re-think the need for a HOC when working with hooks, the purpose of HOC's is to share logic across components, hooks solve the same challenge (in a much better way).

It's hard to understand what logic you tried to share with your HOC in your example but i would go for a custom hook instead.

From the DOCS:

Traditionally in React, we’ve had two popular ways to share stateful logic between components: render props and higher-order components. We will now look at how Hooks solve many of the same problems without forcing you to add more components to the tree.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React - should Higher Order Component be written as a function?

"this" in react higher order component

react higher order component

How to create higher order function in react functional component

React identity higher order component

exporting a higher order component in react

React component name in Higher Order Component

React Higher Order Component not rendering passed component

Decorate imported React component with higher order component

React Higher Order Component - call function in the wrapped component from the wrapper component

React higher order component not working in typescript

Change props in Higher-Order-Component in react

Writing a React higher-order component with TypeScript

React Typescript: exclude property in higher order component

Higher Order Component (self) wrapping in React

Higher Order React Component for Click Tracking

Infinite Loop With Higher Order React Redux Component

React Private Route higher order component?

React JS Higher-Order-Component not execute function if state is being set outside that function

React: Is it possible to call a higher-order component within a container component?

React: Include new prop to the Child Component in Higher Order Component

React Higher Order Component forces re-render of wrapped component

Stop React Higher Order Component from mounting before wrapped component?

Higher Order Component: function keyword vs arrow function

React Higher Order Component explained with Context API example

Pass props to higher-order stateless functional React component

Generic higher-order React component gives type errors

call instance method from higher order component in react

How to reset form from higher order component in React?