-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CRS-292 Prevent state updates on unmounted Channel component (#566)
* Add ref to keep track of whether component is mounted. Do not trigger state update if component has already been unmounted. * Separate logic into hook and add test Co-authored-by: jaapbakker88 <jaap@getstream.io>
- Loading branch information
Tom Hutman
and
jaapbakker88
authored
Oct 14, 2020
1 parent
0fa172a
commit 6de4b41
Showing
3 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import useIsMounted from '../hooks/useIsMounted'; | ||
|
||
describe('useIsMounted hook', () => { | ||
it('should set the value to false after unmounting', () => { | ||
const renderResult = renderHook(() => useIsMounted()); | ||
const ref = renderResult.result.current; | ||
expect(ref.current).toBe(true); | ||
act(() => { | ||
renderResult.unmount(); | ||
}); | ||
expect(ref.current).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useRef, useEffect } from 'react'; | ||
|
||
export default () => { | ||
const isMounted = useRef(true); | ||
useEffect(() => { | ||
return () => { | ||
isMounted.current = false; | ||
}; | ||
}, []); | ||
return isMounted; | ||
}; |