Skip to content

Commit 9833a24

Browse files
authored
Components: refactor Notice to pass exhaustive-deps (#44157)
1 parent ffa4819 commit 9833a24

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

packages/components/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `NavigationMenu` updated to ignore `react/exhaustive-deps` eslint rule ([#44090](https://github.com/WordPress/gutenberg/pull/44090)).
1717
- `RangeControl`: updated to pass `react/exhaustive-deps` eslint rule ([#44271](https://github.com/WordPress/gutenberg/pull/44271)).
1818
- `UnitControl` updated to pass the `react/exhaustive-deps` eslint rule ([#44161](https://github.com/WordPress/gutenberg/pull/44161)).
19+
- `Notice`: updated to satisfy `react/exhaustive-deps` eslint rule ([#44157](https://github.com/WordPress/gutenberg/pull/44157))
1920

2021
## 21.0.0 (2022-09-13)
2122

packages/components/src/notice/index.native.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { BlurView } from '@react-native-community/blur';
1414
/**
1515
* WordPress dependencies
1616
*/
17-
import { useEffect, useRef, Platform } from '@wordpress/element';
17+
import { useEffect, useRef, useCallback, Platform } from '@wordpress/element';
1818
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
1919

2020
/**
@@ -30,40 +30,37 @@ const Notice = ( { onNoticeHidden, content, id, status } ) => {
3030
const timer = useRef( null );
3131

3232
useEffect( () => {
33-
startAnimation();
34-
35-
return () => {
36-
clearTimeout( timer?.current );
37-
};
38-
}, [] );
39-
40-
function onHide() {
33+
// start animation
4134
Animated.timing( animationValue, {
42-
toValue: 0,
43-
duration: 150,
35+
toValue: 1,
36+
duration: 300,
4437
useNativeDriver: true,
4538
easing: Easing.out( Easing.quad ),
4639
} ).start( ( { finished } ) => {
4740
if ( finished && onNoticeHidden ) {
48-
onNoticeHidden( id );
41+
timer.current = setTimeout( () => {
42+
onHide();
43+
}, HIDE_TIMER );
4944
}
5045
} );
51-
}
5246

53-
function startAnimation() {
47+
return () => {
48+
clearTimeout( timer?.current );
49+
};
50+
}, [ animationValue, onHide, onNoticeHidden ] );
51+
52+
const onHide = useCallback( () => {
5453
Animated.timing( animationValue, {
55-
toValue: 1,
56-
duration: 300,
54+
toValue: 0,
55+
duration: 150,
5756
useNativeDriver: true,
5857
easing: Easing.out( Easing.quad ),
5958
} ).start( ( { finished } ) => {
6059
if ( finished && onNoticeHidden ) {
61-
timer.current = setTimeout( () => {
62-
onHide();
63-
}, HIDE_TIMER );
60+
onNoticeHidden( id );
6461
}
6562
} );
66-
}
63+
}, [ animationValue, onNoticeHidden, id ] );
6764

6865
const noticeSolidStyles = usePreferredColorSchemeStyle(
6966
styles.noticeSolid,

packages/components/src/notice/list.native.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { store as noticesStore } from '@wordpress/notices';
1414
*/
1515
import Notice from './';
1616
import styles from './style.scss';
17+
import { useCallback } from '@wordpress/element';
1718

1819
function NoticeList() {
1920
const { notices } = useSelect( ( select ) => {
@@ -25,9 +26,12 @@ function NoticeList() {
2526

2627
const { removeNotice } = useDispatch( noticesStore );
2728

28-
function onRemoveNotice( id ) {
29-
removeNotice( id );
30-
}
29+
const onRemoveNotice = useCallback(
30+
( id ) => {
31+
removeNotice( id );
32+
},
33+
[ removeNotice ]
34+
);
3135

3236
if ( ! notices.length ) {
3337
return null;

0 commit comments

Comments
 (0)