Skip to content

Commit

Permalink
Components: refactor Notice to pass exhaustive-deps (#44157)
Browse files Browse the repository at this point in the history
  • Loading branch information
chad1008 authored Sep 21, 2022
1 parent ffa4819 commit 9833a24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `NavigationMenu` updated to ignore `react/exhaustive-deps` eslint rule ([#44090](https://github.com/WordPress/gutenberg/pull/44090)).
- `RangeControl`: updated to pass `react/exhaustive-deps` eslint rule ([#44271](https://github.com/WordPress/gutenberg/pull/44271)).
- `UnitControl` updated to pass the `react/exhaustive-deps` eslint rule ([#44161](https://github.com/WordPress/gutenberg/pull/44161)).
- `Notice`: updated to satisfy `react/exhaustive-deps` eslint rule ([#44157](https://github.com/WordPress/gutenberg/pull/44157))

## 21.0.0 (2022-09-13)

Expand Down
37 changes: 17 additions & 20 deletions packages/components/src/notice/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { BlurView } from '@react-native-community/blur';
/**
* WordPress dependencies
*/
import { useEffect, useRef, Platform } from '@wordpress/element';
import { useEffect, useRef, useCallback, Platform } from '@wordpress/element';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';

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

useEffect( () => {
startAnimation();

return () => {
clearTimeout( timer?.current );
};
}, [] );

function onHide() {
// start animation
Animated.timing( animationValue, {
toValue: 0,
duration: 150,
toValue: 1,
duration: 300,
useNativeDriver: true,
easing: Easing.out( Easing.quad ),
} ).start( ( { finished } ) => {
if ( finished && onNoticeHidden ) {
onNoticeHidden( id );
timer.current = setTimeout( () => {
onHide();
}, HIDE_TIMER );
}
} );
}

function startAnimation() {
return () => {
clearTimeout( timer?.current );
};
}, [ animationValue, onHide, onNoticeHidden ] );

const onHide = useCallback( () => {
Animated.timing( animationValue, {
toValue: 1,
duration: 300,
toValue: 0,
duration: 150,
useNativeDriver: true,
easing: Easing.out( Easing.quad ),
} ).start( ( { finished } ) => {
if ( finished && onNoticeHidden ) {
timer.current = setTimeout( () => {
onHide();
}, HIDE_TIMER );
onNoticeHidden( id );
}
} );
}
}, [ animationValue, onNoticeHidden, id ] );

const noticeSolidStyles = usePreferredColorSchemeStyle(
styles.noticeSolid,
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/notice/list.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { store as noticesStore } from '@wordpress/notices';
*/
import Notice from './';
import styles from './style.scss';
import { useCallback } from '@wordpress/element';

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

const { removeNotice } = useDispatch( noticesStore );

function onRemoveNotice( id ) {
removeNotice( id );
}
const onRemoveNotice = useCallback(
( id ) => {
removeNotice( id );
},
[ removeNotice ]
);

if ( ! notices.length ) {
return null;
Expand Down

0 comments on commit 9833a24

Please sign in to comment.