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

I was going through the hooks documentation when I stumbled upon useRef. Looking at their example… function TextInputWithFocusButton() { const inputEl = useRef(null); const onButtonClick = () => { // `current` points to the mounted text input element inputEl.current.focus(); }; return ( <> <input ref={inputEl} type=”text” /> <button onClick={onButtonClick}>Focus the input</button> </> ); } …it … Read more

Make React useEffect hook not run on initial render

According to the docs: componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render. We can use the new useEffect() hook to simulate componentDidUpdate(), but it seems like useEffect() is being ran after every render, even the first time. How do I get it to not run on initial … Read more

React Hook “useState” is called in function “app” which is neither a React function component or a custom React Hook function

I’m trying to use react hooks for a simple problem const [personState,setPersonState] = useState({ DefinedObject }); with following dependencies. “dependencies”: { “react”: “^16.8.6”, “react-dom”: “^16.8.6”, “react-scripts”: “3.0.0” } but I’m still getting the following error: ./src/App.js Line 7: React Hook “useState” is called in function “app” which is neither a React function component or a … Read more

How to compare oldValues and newValues on React Hooks useEffect?

Let’s say I have 3 inputs: rate, sendAmount, and receiveAmount. I put that 3 inputs on useEffect diffing params. The rules are: If sendAmount changed, I calculate receiveAmount = sendAmount * rate If receiveAmount changed, I calculate sendAmount = receiveAmount / rate If rate changed, I calculate receiveAmount = sendAmount * rate when sendAmount > … Read more

React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing

I was trying the useEffect example something like below: useEffect(async () => { try { const response = await fetch(`https://www.reddit.com/r/${subreddit}.json`); const json = await response.json(); setPosts(json.data.children.map(it => it.data)); } catch (e) { console.error(e); } }, []); and I get this warning in my console. But the cleanup is optional for async calls I think. I … Read more

How to fix missing dependency warning when using useEffect React Hook

With React 16.8.6 (it was good on previous version 16.8.3), I get this error when I attempt to prevent an infinite loop on a fetch request: ./src/components/BusinessesList.js Line 51: React Hook useEffect has a missing dependency: ‘fetchBusinesses’. Either include it or remove the dependency array react-hooks/exhaustive-deps I’ve been unable to find a solution that stops … Read more