Skip to content

Commit

Permalink
Memoize the active route
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 11, 2024
1 parent da9f6d0 commit 3ae6f53
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions packages/edit-site/src/components/layout/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useEffect } from '@wordpress/element';
import { useEffect, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
Expand Down Expand Up @@ -61,24 +61,26 @@ function useRedirectOldPaths() {
}, [ history, params ] );
}

export default function useLayoutAreas() {
export default function useActiveRoute() {
const { params } = useLocation();
useRedirectOldPaths();
const routes = useSelect( ( select ) => {
return unlock( select( editSiteStore ) ).getRoutes();
}, [] );
const matchedRoute = routes.find( ( route ) => route.match( params ) );
if ( ! matchedRoute ) {
return useMemo( () => {
const matchedRoute = routes.find( ( route ) => route.match( params ) );
if ( ! matchedRoute ) {
return {
key: 404,
areas: {},
widths: {},
};
}

return {
key: 404,
areas: {},
widths: {},
key: matchedRoute.name,
areas: matchedRoute.areas,
widths: matchedRoute.widths,
};
}

return {
key: matchedRoute.name,
areas: matchedRoute.areas,
widths: matchedRoute.widths,
};
}, [ routes, params ] );
}

0 comments on commit 3ae6f53

Please sign in to comment.