Higher-order component is a function that takes a component and
returns a new component.
Lets break down your code to understand what are the two functions, props and children are
const Name = (props) => <span>{props.children}</span>
Name is simply a function component now, so calling it like
<Name>Stack OverFlow<Name/>
will render <span>Stack OverFlow</span> to the dom.
Now lets look at the hoc,
const reverse = (PassedComponent) =>
({ children, ...props }) =>
<PassedComponent {...props}>
{children.split("").reverse().join("")}
</PassedComponent>
reverse is simply function returning another function. The good old way to write it is
var reverse = function reverse(PassedComponent) {
return function (props) {
var children = props.children,
propsWithoutChildren = _objectWithoutProperties(props, ["children"]); //some function to remove children from props
return (
<PassedComponent {...propsWithoutChildren}>
{children.split("").reverse().join("")}
</PassedComponent>
)
};
};
Now when you call const reversedName = reverse(name), reversed name will be a new component which is the function that is returned from the HOC which is equivalent to
const ReversedName = ({ children, ...props }) =>
<Name {...props}> //the component you passed is used here
{children.split("").reverse().join("")}
</Name>
Passing {...props} allows you to pass any additional props to the name component. For eg, if you use the reversed name like this,
<ReversedName className='class-for-name-component'>name</ReversedName>
the className prop will be passed down to the name component. The whole idea is enabling reusability, as here you are rendering the same component Name to render name in both straight and reversed format. Hope this helps.