use-effect

What is the right way to cancel all async/await tasks within an useEffect hook to prevent memory leaks in react?

帅比萌擦擦* 提交于 2020-12-30 08:40:11
问题 I am working on a react chap app that pulls data from a firebase database. In my "Dashboard" component I have an useEffect hook checking for an authenticated user and if so, pull data from firebase and set the state of a an email variable and chats variable. I use abortController for my useEffect cleanup, however whenever I first log out and log back in I get a memory leak warning. index.js:1375 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it

cannot mock function in useEffect when toMatchSnapshot in Jest

旧巷老猫 提交于 2020-12-15 04:28:50
问题 I'm trying to get all snapshot on my LayoutDummy.js to complete my unit test coverage when I Run Test I got error, it says dummyFunc is not a function like below I have wrote my actual code in functional component and testing file like this LayoutDummy.js import React, { useEffect, useState } from 'react'; const LayoutDummy = () => { const [caption, setCaption] = useState('loading...'); useEffect(() => { dummyFunc(); }, []); const dummyFunc = () => { setCaption('dummyFunc is running'); };

useEffect not called in React Native when back to screen

那年仲夏 提交于 2020-12-02 07:13:15
问题 How are you. This is scenario of this issue. Let's say there are 2 screens to make it simple. enter A screen. useEffect of A screen called. navigate to B screen from A screen navigate back to A screen from B. at this time, useEffect is not called. function CompanyComponent(props) { const [roleID, setRoleID] = useState(props.user.SELECTED_ROLE.id) useEffect(()=>{ // this called only once when A screen(this component) loaded, // but when comeback to this screen, it doesn't called setRoleID

useEffect not called in React Native when back to screen

谁说胖子不能爱 提交于 2020-12-02 07:13:03
问题 How are you. This is scenario of this issue. Let's say there are 2 screens to make it simple. enter A screen. useEffect of A screen called. navigate to B screen from A screen navigate back to A screen from B. at this time, useEffect is not called. function CompanyComponent(props) { const [roleID, setRoleID] = useState(props.user.SELECTED_ROLE.id) useEffect(()=>{ // this called only once when A screen(this component) loaded, // but when comeback to this screen, it doesn't called setRoleID

useEffect not called in React Native when back to screen

我的梦境 提交于 2020-12-02 07:12:52
问题 How are you. This is scenario of this issue. Let's say there are 2 screens to make it simple. enter A screen. useEffect of A screen called. navigate to B screen from A screen navigate back to A screen from B. at this time, useEffect is not called. function CompanyComponent(props) { const [roleID, setRoleID] = useState(props.user.SELECTED_ROLE.id) useEffect(()=>{ // this called only once when A screen(this component) loaded, // but when comeback to this screen, it doesn't called setRoleID

React Hooks: Handling Objects as dependency in useEffects

混江龙づ霸主 提交于 2020-11-28 09:15:33
问题 UPDATE: Yes for use case 1, if I extract search.value outside the useEffect and use it as a dependency it works. But I have an updated Use case below Use Case 2 : I want to pass a searchHits Object to the server. The server in turn return it back to me with an updated value in response. If I try using the searchHits Object I still get the infinite loop state: { visible: true, loading: false, search: { value: “”, searchHits: {....}, highlight: false, } } let val = search.value let hits =

React Hooks: Handling Objects as dependency in useEffects

倖福魔咒の 提交于 2020-11-28 09:13:05
问题 UPDATE: Yes for use case 1, if I extract search.value outside the useEffect and use it as a dependency it works. But I have an updated Use case below Use Case 2 : I want to pass a searchHits Object to the server. The server in turn return it back to me with an updated value in response. If I try using the searchHits Object I still get the infinite loop state: { visible: true, loading: false, search: { value: “”, searchHits: {....}, highlight: false, } } let val = search.value let hits =