useReducer Action dispatched twice

To first clarify the existing behavior, the STATUS_FETCHING action was actually only being “dispatched” (i.e. if you do a console.log right before the dispatch call in getData within useApiCall.js) once, but the reducer code was executing twice. I probably wouldn’t have known what to look for to explain why if it hadn’t been for my … Read more

Correct way to create event handlers using hooks in React?

I wouldn’t recommend either useState or useRef. You don’t actually need any hook here at all. In many cases, I’d recommend simply doing this: const MyComponent = () => { const handleClick = (e) => { //… } return <button onClick={handleClick}>Click Me</button>; }; However, it’s sometimes suggested to avoid declaring functions inside a render function … Read more

What’s useEffect execution order and its internal clean-up logic when requestAnimationFrame and cancelAnimationFrame are used?

One thing that’s not clear in the above answers is the order in which the effects run when you have multiple components in the mix. We’ve been doing work that involves coordination between a parent and it’s children via useContext so the order matters more to us. useLayoutEffect and useEffect work in different ways in … Read more

What’s the difference between useState and useEffect?

To put it simply, both useState and useEffect enhance functional components to make them do things that classes can but functional components (without hooks) cannot: useState allows functional components to have state, like this.state in class components. useEffect allows functional components to have lifecycle methods (such as componentDidMount, componentDidUpdate and componentWillUnmount) in one single API. … Read more

How to add “refs” dynamically with react hooks?

Not sure i fully understand your intent, but i think you want something like this: const { useState, useRef, createRef, useEffect } = React; const data = [ { text: “test1” }, { text: “test2” } ]; const Component = () => { const [heights, setHeights] = useState([]); const elementsRef = useRef(data.map(() => createRef())); useEffect(() … Read more

Functional Component: Write functions inside or outside the component?

This question is pretty opinion-based but there are few notes that you need to think about. Declaring the function outside of scope is foremost for readability and reusability. // Reuse logic in other components const counterAsFloat = (counter) => { return counter.toFixed(2); }; // If Counter has complex logic, you sometimes want to compose it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)