The wrapper function takes two inputs, one is the component and the other is the class name. My question is, why didn't we directly use jsx codes after return? Why did we put an arrow function after return? If I use jsx code directly after return, I get the following error
import React from "react";
const Wrapper = (WrappedComponent, className) => {
return (props) => (
<div className={className}>
<WrappedComponent />
</div>
);
};
export default Wrapper;
a higher order component,
Wrapperin your case, will be used as follows:so see how the
WrapperComponentis being rendered? it's being rendered as a normal react component, i.e. this syntax<WrapperComponent />. Thus, like any other react component,WrappedComponentcan either be a class component or as in this case, a functional component (returned byWrapped).if
Wrappedwas to return the jsx directly as you said, you wouldn't be able to renderWrappedComponentas react component! You would have to do the below (which is usually a counter pattern in react, and not how HOC should be used)