Skip to content

Commit

Permalink
Merge pull request #28 from asmyshlyaev177/fix_setUrl_options_def
Browse files Browse the repository at this point in the history
fix: fix `setUrl` options as second argument
  • Loading branch information
asmyshlyaev177 authored Nov 14, 2024
2 parents 518e7bb + 2347850 commit 35c4522
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/example-nextjs14/src/app/Form-for-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const Form = ({
setState((st) => ({ ...st, age: 18 }));
setUrl(urlState);
setUrl((st) => ({ ...st, age: 18 }));
setUrl(urlState, { replace: true });
setUrl((st) => ({ ...st, age: 18 }), { replace: true });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
2 changes: 2 additions & 0 deletions packages/example-nextjs15/src/app/Form-for-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const Form = ({
setState((st) => ({ ...st, age: 18 }));
setUrl(urlState);
setUrl((st) => ({ ...st, age: 18 }));
setUrl(urlState, { replace: true });
setUrl((st) => ({ ...st, age: 18 }), { replace: true });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
2 changes: 2 additions & 0 deletions packages/example-react-router6/src/Form-for-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const Form = ({ className }: { className?: string }) => {
setState((st) => ({ ...st, age: 18 }));
setUrl(urlState);
setUrl((st) => ({ ...st, age: 18 }));
setUrl(urlState, { replace: true });
setUrl((st) => ({ ...st, age: 18 }), { replace: true });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
14 changes: 9 additions & 5 deletions packages/urlstate/next/useUrlState/useUrlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ export function useUrlState<T extends JSONCompatible>({
* NextJS hook. Returns `urlState`, `setState`, and `setUrl` functions
*
* @param {JSONCompatible<T>} [defaultState] Fallback (default) values for state
* @param {Object} params - Object with other parameters
* @param {?SearchParams<T>} params.searchParams searchParams from Next server component
* @param {Object} params - Object with other parameters, including params from App router
* @param {boolean} params.replace replace URL of push, default `true`
* @param {boolean} params.useHistory use window.history for navigation, default true, no _rsc requests https://github.com/vercel/next.js/discussions/59167
* @param {?SearchParams<T>} params.searchParams searchParams from Next server component
* @param {boolean} params.scroll reset scroll, default `false`
*
* * Example:
* ```ts
Expand All @@ -49,9 +52,7 @@ export function useUrlState<T extends JSONCompatible>({
* const { urlState, setState, setUrl } = useUrlState(form, { searchParams });
*
* setState({ name: 'test' });
* // by default it's uses router.push with scroll: false
* setUrl({ name: 'test' }, { replace: true, scroll: true });
* // similar to React.useState
* setUrl(curr => ({ ...curr, name: 'test' }), { replace: true, scroll: true });
* ```
*
Expand All @@ -63,7 +64,10 @@ export function useUrlState<T extends JSONCompatible>(
): {
urlState: T;
setState: (value: Partial<T> | ((currState: T) => T)) => void;
setUrl: (value?: Partial<T> | ((currState: T) => T)) => void;
setUrl: (
value?: Partial<T> | ((currState: T) => T),
options?: Options,
) => void;
};

export function useUrlState<T extends JSONCompatible>(
Expand Down
13 changes: 9 additions & 4 deletions packages/urlstate/react-router/useUrlState/useUrlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function useUrlState<T extends JSONCompatible>({
* @param {JSONCompatible<T>} [defaultState] Fallback (default) values for state
* @param {Object} params - Object with other parameters
* @param {NavigateOptions} params.NavigateOptions See type from `react-router-dom`
* @param {boolean} params.useHistory use window.history for navigation
* @param {boolean} params.replace replace URL of push, default `true`
* @param {boolean} params.useHistory use window.history for navigation, default `false`
* @param {boolean} params.preventScrollReset keep scroll position, default `true`
* * Example:
* ```ts
* export const form = { name: '', age: 0 };
Expand All @@ -61,7 +63,10 @@ export function useUrlState<T extends JSONCompatible>(
): {
urlState: T;
setState: (value: Partial<T> | ((currState: T) => T)) => void;
setUrl: (value?: Partial<T> | ((currState: T) => T)) => void;
setUrl: (
value?: Partial<T> | ((currState: T) => T),
options?: Params,
) => void;
};

export function useUrlState<T extends JSONCompatible>(
Expand Down Expand Up @@ -140,7 +145,7 @@ export function useUrlState<T extends JSONCompatible>(
* // or
* setState(curr => ({ ...curr, name: 'test' }) );
* // can pass optional React-Router `NavigateOptions`
* setState(curr => ({ ...curr, name: 'test', preventScrollReset: false }) );
* setState(curr => ({ ...curr, name: 'test' }) );
* ```
*
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/react-router/useUrlState#updatestate}
Expand All @@ -157,7 +162,7 @@ export function useUrlState<T extends JSONCompatible>(
* // or
* setUrl(curr => ({ ...curr, name: 'test' }), { replace: true } );
* // can pass optional React-Router `NavigateOptions`
* setState(curr => ({ ...curr, name: 'test', preventScrollReset: false }) );
* setUrl(curr => ({ ...curr, name: 'test' }), { preventScrollReset: false } );
* ```
*
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/react-router/useUrlState#updateurl}
Expand Down

0 comments on commit 35c4522

Please sign in to comment.