Skip to content

Commit

Permalink
Merge pull request #1336 from Chia-Network/queryparam-hook-update
Browse files Browse the repository at this point in the history
fix: page query param set when navigating to a paginated page
  • Loading branch information
TheLastCicada authored Nov 18, 2024
2 parents 7684ff6 + 8ba7cb8 commit 0849e8b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/renderer/hooks/useQueryParamState.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import { useState, useCallback, useMemo } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation } from 'react-router-dom';

// Define a TypeScript interface for the function's return type
Expand Down Expand Up @@ -35,6 +35,14 @@ const useQueryParamState: QueryParamState<string> = (name, defaultValue = '') =>
return params.get(name) || defaultValue;
}, [name, location, param, defaultValue]);

// Ensure default value is set in URL on initial mount if it's missing
useEffect(() => {
const params = new URLSearchParams(window.location.search || window.location.hash.replace(/#.*\?/, ''));
if (!params.has(name) && defaultValue) {
setQueryStringParameter(defaultValue);
}
}, [name, defaultValue, setQueryStringParameter]);

return [value, setQueryStringParameter];
};

Expand Down

0 comments on commit 0849e8b

Please sign in to comment.