matthewp / haunted

React's Hooks API implemented for web components 👻

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Higher Order Components with Haunted

nirbG opened this issue · comments

Hey guys, someone already tried to create a Higher Order Component with haunted? can you share with me some example, pls

I might find a solution,

const hoc = (WrappedComponent) => (props) => {
  // here I update my props with a custom hooks
  const newProps = useProps(props);
  return WrappedComponent(newProps);
};

it works well, I get the right props, but now "this" is undefined in my component.
I tried a lot of things but nothing works.
does anyone have any ideas on this?

props is itself the element. You can do return WrappedComponent.call(newProps, newProps) and it should have the correct this.

where the "call" function come from?

All functions have a .call function on them. It only works if the WrapperComponent is defined like function WrapperComponent() and not const WrapperComponent = () =>.