react-hooks

Why does calling useState's setter with the same value subsequently trigger a component update even if the old state equals the new state?

谁都会走 提交于 2021-02-07 13:19:38
问题 This problem occurs only if the state value was actually changed due to the previous update. In the following example, when the button is clicked for the first time, "setState" is called with a new value (of 12), and a component update occurs, which is understandable. When I click the same button for the second time, setting the state to the same value of 12 it causes the component to re-run (re-render), and why exactly that happens is my main question. Any subsequent setStates to the same

Definition for rule 'react-hooks/exhaustive-deps' was not found

心已入冬 提交于 2021-02-07 04:53:06
问题 I am getting the following eslint error after adding // eslint-disable-next-line react-hooks/exhaustive-deps in my code. 8:14 error Definition for rule 'react-hooks/exhaustive-deps' was not found I referred to this post to fix this but the solution mentioned doesn't work in my case. Any clue how to suppress this eslint error? PS I'm using standardjs in conjuction. 回答1: This typically happens because the react-hooks plugin is missing in the .eslintrc plugin configuration. Ensure you have added

Avoid old data when using useEffect to fetch data

Deadly 提交于 2021-02-07 03:37:30
问题 My problem is, when a custom hook uses useEffect with useState (e.g. to fetch data), the custom hook returns stale data (from the state), after dependencies change but before useEffect is fired. Can you suggest a right/idiomatic way to resolve that? I'm using the React documentation and these articles to guide me: A Complete Guide to useEffect How to fetch data with React Hooks? I defined a function, which uses useEffect and which is meant to wrap the fetching of data -- the source code is

Avoid old data when using useEffect to fetch data

久未见 提交于 2021-02-07 03:32:39
问题 My problem is, when a custom hook uses useEffect with useState (e.g. to fetch data), the custom hook returns stale data (from the state), after dependencies change but before useEffect is fired. Can you suggest a right/idiomatic way to resolve that? I'm using the React documentation and these articles to guide me: A Complete Guide to useEffect How to fetch data with React Hooks? I defined a function, which uses useEffect and which is meant to wrap the fetching of data -- the source code is

Avoid old data when using useEffect to fetch data

风格不统一 提交于 2021-02-07 03:31:52
问题 My problem is, when a custom hook uses useEffect with useState (e.g. to fetch data), the custom hook returns stale data (from the state), after dependencies change but before useEffect is fired. Can you suggest a right/idiomatic way to resolve that? I'm using the React documentation and these articles to guide me: A Complete Guide to useEffect How to fetch data with React Hooks? I defined a function, which uses useEffect and which is meant to wrap the fetching of data -- the source code is

React batch updates for multiple setState() calls inside useEffect hook

梦想与她 提交于 2021-02-06 20:09:24
问题 On this answer by Dan Abramov here on SO, I've found out the following: Does React keep the order for state updates? Currently (React 16 and earlier), only updates inside React event handlers are batched by default. There is an unstable API to force batching outside of event handlers for rare cases when you need it. He also mentions that in this Github issue: https://github.com/facebook/react/issues/10231#issuecomment-316644950 In current release, they will be batched together if you are

React batch updates for multiple setState() calls inside useEffect hook

别等时光非礼了梦想. 提交于 2021-02-06 19:58:30
问题 On this answer by Dan Abramov here on SO, I've found out the following: Does React keep the order for state updates? Currently (React 16 and earlier), only updates inside React event handlers are batched by default. There is an unstable API to force batching outside of event handlers for rare cases when you need it. He also mentions that in this Github issue: https://github.com/facebook/react/issues/10231#issuecomment-316644950 In current release, they will be batched together if you are

What's the difference between `useCallback` with an empty array as inputs and `useCallback` without a second parameter?

感情迁移 提交于 2021-02-05 21:25:37
问题 On my journey to try and understand React Hooks better I came across some behaviour I did not expect. I was attempting to create an array of refs and pushing to said array via an onRef function I would pass to my <div>'s . The array kept getting bigger everytime the component re-rendered presumably just because it was a simple arrow function and not memoized. So then I added the useCallback hook to make sure that I wouldn't get the same ref multiple times, but to my surprise it still called

What's the difference between `useCallback` with an empty array as inputs and `useCallback` without a second parameter?

泪湿孤枕 提交于 2021-02-05 21:20:28
问题 On my journey to try and understand React Hooks better I came across some behaviour I did not expect. I was attempting to create an array of refs and pushing to said array via an onRef function I would pass to my <div>'s . The array kept getting bigger everytime the component re-rendered presumably just because it was a simple arrow function and not memoized. So then I added the useCallback hook to make sure that I wouldn't get the same ref multiple times, but to my surprise it still called

Push a new value to nested array in React hooks

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 08:15:26
问题 I have a function onTagSelected() that updates now a single value for a key inside a object: const NotesContainer = ({ }) => { const [notesDummyData, setNotesDummyData] = useState([ { id: '5', title: 'Sauna', tag: 'relax', }, { id: '7', title: 'Finland', tag: 'nordics' }, ]); const onTagSelected = (selectedTag, itemId) => { setNotesDummyData(notesDummyData.map((x) => { if (x.id !== itemId) return x; return { ...x, tag: selectedTag }; })); }; Now I would like to change the function and be able