react-hooks

Why is my react component rendering twice on initial load?

ぐ巨炮叔叔 提交于 2021-01-20 07:26:07
问题 I have a functional component called (First) function First() { const [count,setCount]=useState(0) console.log("component first rendering") // this logging is happening twice return ( <div> first component </div> ) } when i initially run the application the console statement is logging twice why is it, It should have been logged only once, because i haven't explicitily updated the state. 回答1: I've tried this out in code sandbox here and sure enough, it did render twice. This is because, in

Why is my react component rendering twice on initial load?

拥有回忆 提交于 2021-01-20 07:25:12
问题 I have a functional component called (First) function First() { const [count,setCount]=useState(0) console.log("component first rendering") // this logging is happening twice return ( <div> first component </div> ) } when i initially run the application the console statement is logging twice why is it, It should have been logged only once, because i haven't explicitily updated the state. 回答1: I've tried this out in code sandbox here and sure enough, it did render twice. This is because, in

IntersectionObserver with React & Hooks

一个人想着一个人 提交于 2021-01-19 06:27:14
问题 I'm trying to track element visibility with React/Hooks and the Intersection Observer API. However, I can't figure out how to set up observation with "useEffect". Does anybody have any idea how could I do that? Mine solution does not work... function MainProvider({ children }) { const [targetToObserve, setTargetToObserve] = useState([]); window.addEventListener("load", () => { const findTarget = document.querySelector("#thirdItem"); setTargetToObserve([findTarget]); }); useEffect(() => {

How to send request on click React Hooks way?

坚强是说给别人听的谎言 提交于 2021-01-16 06:45:53
问题 How to send http request on button click with react hooks? Or, for that matter, how to do any side effect on button click? What i see so far is to have something "indirect" like: export default = () => { const [sendRequest, setSendRequest] = useState(false); useEffect(() => { if(sendRequest){ //send the request setSendRequest(false); } }, [sendRequest]); return ( <input type="button" disabled={sendRequest} onClick={() => setSendRequest(true)} ); } Is that the proper way or is there some other

How to send request on click React Hooks way?

别等时光非礼了梦想. 提交于 2021-01-16 06:44:33
问题 How to send http request on button click with react hooks? Or, for that matter, how to do any side effect on button click? What i see so far is to have something "indirect" like: export default = () => { const [sendRequest, setSendRequest] = useState(false); useEffect(() => { if(sendRequest){ //send the request setSendRequest(false); } }, [sendRequest]); return ( <input type="button" disabled={sendRequest} onClick={() => setSendRequest(true)} ); } Is that the proper way or is there some other

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

How to avoid coupling when using useReducer?

蹲街弑〆低调 提交于 2021-01-05 06:16:08
问题 To prevent passing callbacks to child components I am using useReducer instead. This avoids the issue of child components re-rendering on each parent render but the downside seems to be a tight coupling between parent and child. By tight coupling, I mean that the child needs to be explicitly aware of the shape of the action expected by the reducer which is defined by the parent. Imagine, for example, a date picker component. At some point this component needs to pass the new date/time value

How can I make this a callable utils function that contains a hook?

岁酱吖の 提交于 2021-01-04 06:36:09
问题 I'm currently using Apollo Client's useQuery and useMutation hooks to perform the same function in several different components. Instead of repeating the logic, I would like to create a helper function that can keep the logic in a single place. The problem with this, is useQuery and useMutation being hooks mean they can't just live in a separate file as functions, but instead need to be valid React function components. I started trying to make something functional but starting to think this