Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Restoration/23fa309daefbd1b3397733e13b38c560 to your computer and use it in GitHub Desktop.
Save Restoration/23fa309daefbd1b3397733e13b38c560 to your computer and use it in GitHub Desktop.
import React from 'react';
import { withRouter } from 'next/router';
// 型修正
export const withComponentRouter = (Component: any) => {
return withRouter(({ router, ...props }) => {
// split at first `?`
const searchParams = new URLSearchParams(router.asPath.split(/\?/)[1]);
const query: { [key: string]: string } = {};
// 型修正
for (const [key, value] of searchParams as any) {
query[key] = value;
}
// replace the empty query
router.query = query;
return <Component {...props} router={router} />;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment