use-effect

React setInterval in useEffect with setTimeout delay

北城以北 提交于 2021-02-08 07:32:19
问题 I want to run an interval with a delay for the first time it fires. How can I do this with useEffect? Because of the syntax I've found it difficult to achieve what I want to do The interval function useEffect(()=>{ const timer = setInterval(() => { //do something here return ()=> clearInterval(timer) }, 1000); },[/*dependency*/]) The delay function useEffect(() => { setTimeout(() => { //I want to run the interval here, but it will only run once //because of no dependencies. If i populate the

How do I keep state persistant using local storage, react hooks, and Context Provider

早过忘川 提交于 2021-02-07 20:01:25
问题 I'm implementing authentication into my app, the issue I am having is on refresh the state is not persistent so I get redirected back to the login screen. How can I use React Hooks to check if there is an authToken in local storage to keep me logged in on refresh. This is My Protected Route That checks for auth tokens <Route {...rest} render={ props=> authTokens ? ( <Component {...props}/> ) : ( <Redirect to ={ { pathname: '/login', state: { from: props.location } } } /> This is my Login page

Is the “useEffect has a missing dependency” warning sometimes wrong?

血红的双手。 提交于 2021-02-07 19:52:01
问题 I've been using hooks for a while and I never fully understand why React is forcing me to includes some dependencies that I don't want on my useEffect. The way I understand the 'dependencies' of a useEffect hook Add the values you want to 'listen' whenever they change and trigger your effect. This works perfectly with a simple effect like: import React, {useEffect, useState} from "react"; interface Props { id: string } const SimpleComponent = (props: Props) => { const {id} = props; const

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

While fetching data from JSON Can't perform a React state update on an unmounted component

纵饮孤独 提交于 2021-01-29 20:00:14
问题 I am trying to fetch data from json file in firebase. I get this error before the app loads sometimes. The App still works. Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. Kindly help me to fix this. Here is my code : const [dataSource, setData] = useState([]); useEffect(() => { fetch("https://jsonurlcomeshere

How to dispatch in useEffect?

老子叫甜甜 提交于 2021-01-29 11:32:54
问题 I tried to set a state using useState inside useEffect with no problem, code as follows: import React, {useState, useEffect} from 'react'; const Banner = () => { const [title, setTitle] = useState([]); useEffect(() => { async function fetchData() { const api = 'some api url'; let response = await fetch(api); response = await response.json(); setTitle(response.title); } fetchData(); }) } export default Banner; Now I'd like to do the same thing but with useReducer, I tried to modify the above

react promise in functional component

岁酱吖の 提交于 2021-01-29 05:55:53
问题 I have a functional component in which I want to save the data in my inputs form sending them with saveForm method to my java API. How can I read the response of this promise props.saveForm (in order to look for potentials server errors) in a functional component ? I've read that in these cases it must be used useEffect hooks..but If I move my handleSubmitForm() inside useEffect than I can't call it when the onSubmit event is thrown. Please enlight me. Thank you for your wisdom. Code I found

react promise in functional component

戏子无情 提交于 2021-01-29 05:49:10
问题 I have a functional component in which I want to save the data in my inputs form sending them with saveForm method to my java API. How can I read the response of this promise props.saveForm (in order to look for potentials server errors) in a functional component ? I've read that in these cases it must be used useEffect hooks..but If I move my handleSubmitForm() inside useEffect than I can't call it when the onSubmit event is thrown. Please enlight me. Thank you for your wisdom. Code I found

React useEffect avoid update callback in every update

为君一笑 提交于 2021-01-28 21:46:42
问题 Is there a way to avoid update a callback in useEffect?. For example I am subscribed an event with geofire, this listens for changes and receive locations. I need to update my state without subscribing every time on every update. Current behavior Subscribed to geofire and receive location (A) Update my state with the new location (A) Subscribed to geofire again and receive location (A) this is an infinite loop. And I can't receive the other locations Expected behavior subscribed to geofire