0

Below is an react HOC but I don't understand why such a syntax could work - note that two fat arrows as in Component => props =>?

const withLogging = Component => props => {
  useEffect(() => {
    fetch(`/logger?location=${ window.location}`);
  }, []);
  return <Component {...props } />;
};

And when I tried to convert it to typescript, it gives me errors

const withLogging = (Component: React.Component) =>(props: any)  => {
  useEffect(() => {
    fetch(`/logger?location=${ window.location}`);
  }, []);
  return < Component {...props } />;
};
1
  • You have to add these error messages to your question or we are not able to understand the problem Commented Dec 25, 2022 at 15:37

1 Answer 1

2

I don't understand why there are two fat arrows

This isn't some special syntax, that's just a arrow function returning another arrow function

// a function taking in a i returning that i
i => i

// a function taking in a i, returning another function which takes in a j and returns i + j
i => j => i + j

above is similar to

function(i) { return i; }

function(i) {
    return function(j) {
        return i + j;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I've updated my question while I was trying to convert it to typescript to understand further

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.