use-effect

useEffect in React not update component after update store

China☆狼群 提交于 2021-01-28 09:05:51
问题 I don't understand why React not update my object. In another component through the dispatch I update the state. In this (in code below) code in mapStateToProps categories are changing (console log show one more category). But component not rerender, although in component in useEffect I use props.categories. Event console.log in element does not run const LeftSidebar = (props: any) => { console.log('not render after props.categories changed') useEffect(() => { props.dispatch(getCategories())

Omit function props from the dependencies list in React.useEffect?

99封情书 提交于 2021-01-28 07:32:27
问题 I have the following block of code: function foo({ bar, func }) { useEffect(() => { func(bar) }, [func, bar]) .... } Is this an anti pattern to include func within the dependencies list for React.useEffect ? I'm under the impression that the function definition for func almost never changes. Please correct me if I'm wrong. 回答1: This depends on the value of func that is obviously initialized in the parent component But assuming func is set to a constant function that will never change (which

Omit function props from the dependencies list in React.useEffect?

不想你离开。 提交于 2021-01-28 07:18:33
问题 I have the following block of code: function foo({ bar, func }) { useEffect(() => { func(bar) }, [func, bar]) .... } Is this an anti pattern to include func within the dependencies list for React.useEffect ? I'm under the impression that the function definition for func almost never changes. Please correct me if I'm wrong. 回答1: This depends on the value of func that is obviously initialized in the parent component But assuming func is set to a constant function that will never change (which

First call to ReactContext is setting the isLoading sate to false

笑着哭i 提交于 2021-01-28 06:55:50
问题 I have Component A and Component B that both uses the same ReactContext to fetch the data. Whichever component calls the context first, at the end of fetching the data, the Context sets the isFetchingData state to false . Now, when the second component calls the context the isFetchingData state is already set to false. Therefore, it renders the component without waiting for the data. To illustrate: ComponentA => isFetchingData? true => getsData => sets isFetchingData to false. => renders

Fetching data in React's useEffect() returns “undefined”

 ̄綄美尐妖づ 提交于 2021-01-27 18:38:58
问题 I'm trying to fetch data from a database. It's a get request. All works fine as long I am using the fetched data in the async function. But ouside of it, it just returns "undefined". What am I doing wrong? Thanks for your help :) const [accountInfos, setAccountInfos] = useState(); const [error, setError] = useState(); const [loading, setLoading] = useState(true); useEffect(() => { (async function runEffect() { const fetchedAccountInfos = await fetchAccountInfos(); if (fetchedAccountInfos &&

React Hooks useEffect, adding dependency triggers infinite loop

十年热恋 提交于 2021-01-27 04:18:08
问题 Inside of my useEffect, I have a props dependency (setIsValid). When I add this dependency to the useEffect, it lands in an infinite loop. Parent when Calling Child Component: const setIsValid = (bool) => { const tmpStateCopy = Object.assign({}, state); tmpStateCopy.isValid = bool; setState(tmpStateCopy); }; return <Child setIsValid={setIsValid} /> In the Child Component: const { setIsValid } = props; const [state, setState] = useState({ transformations: [], duplicateIndexes: [] }); const {

How to enable volume slider of Player tone.js inside of useEffect hook(react)?

自作多情 提交于 2021-01-07 03:13:43
问题 I'm currently trying to adjust the volume of a Player component provided by tone.js. I initiated a new Player and utilized useRef to save a reference of the object in current . the Player contains keys like url, loop, volume etc. Outside of the useEffect I have a few eventhandlers which control the play and stop methods as well as a loop button that toggles the state of loop inside useEffect. They all work. The problem I am facing is that whenever I want to adjust the volume, useEffect is

How to enable volume slider of Player tone.js inside of useEffect hook(react)?

醉酒当歌 提交于 2021-01-07 03:13:25
问题 I'm currently trying to adjust the volume of a Player component provided by tone.js. I initiated a new Player and utilized useRef to save a reference of the object in current . the Player contains keys like url, loop, volume etc. Outside of the useEffect I have a few eventhandlers which control the play and stop methods as well as a loop button that toggles the state of loop inside useEffect. They all work. The problem I am facing is that whenever I want to adjust the volume, useEffect is

How to enable volume slider of Player tone.js inside of useEffect hook(react)?

亡梦爱人 提交于 2021-01-07 03:12:59
问题 I'm currently trying to adjust the volume of a Player component provided by tone.js. I initiated a new Player and utilized useRef to save a reference of the object in current . the Player contains keys like url, loop, volume etc. Outside of the useEffect I have a few eventhandlers which control the play and stop methods as well as a loop button that toggles the state of loop inside useEffect. They all work. The problem I am facing is that whenever I want to adjust the volume, useEffect is

What's wrong with my useEffect code, and why is my state variable empty?

穿精又带淫゛_ 提交于 2021-01-05 08:39:25
问题 I have a problem and I'm not sure what's causing the problem. So I have done a fetch with an external api and I want to print out some of the information from the api. I have logged the data and the state variable to see if they return the data. My problem is that I don't get any data from the state variable in the console. When I log it in the console it only shows an empty array. But I get the data in the console when I'm logging console.log(data). When I remove the empty array at the end