Invalid hook call. Hooks can only be called inside of the body of a function component

I had this issue when I used npm link to install my local library, which I’ve built using cra. I found the answer here. Which literally says: This problem can also come up when you use npm link or an equivalent. In that case, your bundler might “see” two Reacts — one in application folder … Read more

In general is it better to use one or many useEffect hooks in a single component? [closed]

The pattern that you need to follow depends on your use case. First: You might have a situation where you need to add event listener during the initial mount and clean them up at unmount and another case where a particular listener needs to be cleaned up and re-added on a prop change. In such … Read more

componentDidMount equivalent on a React function/Hooks component?

For the stable version of hooks (React Version 16.8.0+) For componentDidMount useEffect(() => { // Your code here }, []); For componentDidUpdate useEffect(() => { // Your code here }, [yourDependency]); For componentWillUnmount useEffect(() => { // componentWillUnmount return () => { // Your code here } }, [yourDependency]); So in this situation, you need … Read more

What’s the difference between `useRef` and `createRef`?

The difference is that createRef will always create a new ref. In a class-based component, you would typically put the ref in an instance property during construction (e.g. this.input = createRef()). You don’t have this option in a function component. useRef takes care of returning the same ref each time as on the initial rendering. … Read more

tech