Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix initial page redirection
  • Loading branch information
kevin940726 committed Mar 1, 2022
commit e4029fdbc8ca36d7868e74daef0d99e2a80701b1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { Action } from 'history';

/**
* WordPress dependencies
*/
Expand All @@ -6,19 +11,30 @@ import { useRef, useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useLocation } from './index';
import { useLocation, useHistory } from './index';

export default function useResetFocusOnRouteChange( targetRef ) {
const history = useHistory();
const location = useLocation();
const isInitialPageLoadRef = useRef( true );
const expectRedirectionRef = useRef( false );

useEffect( () => {
// Don't focus for initial page load.
if ( isInitialPageLoadRef.current ) {
isInitialPageLoadRef.current = false;
expectRedirectionRef.current = true;
return;
}
// Don't focus for the initial page redirection.
// TODO: This can be removed once #36873 is resolved.
if ( expectRedirectionRef.current ) {
expectRedirectionRef.current = false;
if ( history.action === Action.Replace ) {
return;
}
}
Comment on lines +29 to +36
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully we can remove all these once #36873 is resolved and make this hook easier to understand.


targetRef.current?.focus();
}, [ location, targetRef ] );
}, [ location, targetRef, history ] );
}