Skip to content

Commit 499a2f8

Browse files
committed
refactor: use url state parameters
BREAKING CHANGE: getEncodedState replaces getLocationHash onEncodedState replaces setLocationHash
1 parent 224fff7 commit 499a2f8

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import { useCallback } from "react";
1515
import { useUrlState } from "react-state-url-fragment";
1616

1717
export function usePageState<T>(defaultState?: T) {
18-
const getLocationHash = useCallback(() => location.hash.substring(1), []);
19-
const setLocationHash = useCallback((hash) => (location.hash = hash), []);
18+
const getEncodedState = useCallback(() => location.hash.substring(1), []);
19+
const onEncodedState = useCallback((hash) => (location.hash = hash), []);
2020
const handleDecodeError = defaultState && (() => defaultState);
2121

2222
return useUrlState<T>({
23-
getLocationHash,
23+
getEncodedState,
2424
handleDecodeError,
25-
setLocationHash,
25+
onEncodedState,
2626
});
2727
}
2828
```

demo/src/components/hooks/useAuthState.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ const AUTH_STORAGE_KEY = "auth-storage-key";
1010
export function useAuthState(
1111
defaultState?: AuthState,
1212
): UseUrlStateResult<AuthState> {
13-
const getLocationHash = useCallback(function getStorageValue() {
13+
const getEncodedState = useCallback(function getStorageValue() {
1414
return window.localStorage.getItem(AUTH_STORAGE_KEY) ?? "";
1515
}, []);
16-
const setLocationHash = useCallback(function setStorageValue(hash: string) {
16+
const onEncodedState = useCallback(function setStorageValue(hash: string) {
1717
window.localStorage.setItem(AUTH_STORAGE_KEY, hash);
1818
}, []);
1919
const handleDecodeError = defaultState && (() => defaultState);
2020

2121
return useUrlState<AuthState>({
22-
getLocationHash,
22+
getEncodedState,
2323
handleDecodeError,
24-
setLocationHash,
24+
onEncodedState,
2525
});
2626
}

demo/src/components/hooks/usePageState.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ type PageState = {
88
export function usePageState<T = PageState>(
99
defaultState?: T,
1010
): UseUrlStateResult<T> {
11-
const getLocationHash = useCallback(() => location.hash.substring(1), []);
12-
const setLocationHash = useCallback((hash: string) => {
11+
const getEncodedState = useCallback(() => location.hash.substring(1), []);
12+
const onEncodedState = useCallback((hash: string) => {
1313
location.hash = hash;
1414
}, []);
1515
const handleDecodeError = defaultState && (() => defaultState);
1616

1717
return useUrlState<T>({
18-
getLocationHash,
18+
getEncodedState,
1919
handleDecodeError,
20-
setLocationHash,
20+
onEncodedState,
2121
});
2222
}

src/hooks/useUrlState.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useUrlState } from "./useUrlState";
66
describe("useUrlState", () => {
77
const emptyUrlStateDecoded = {};
88
const emptyUrlStateEncoded = "e30%3D";
9-
const setLocationHash = jest.fn();
9+
const onEncodedState = jest.fn();
1010
const fallbackEmptyUrlState = jest.fn(() => emptyUrlStateDecoded);
1111
const urlStateEncodedInvalid = "#!!invalid@!";
1212

@@ -27,9 +27,9 @@ describe("useUrlState", () => {
2727

2828
const { result } = renderHook(() =>
2929
useUrlState({
30-
getLocationHash: () => encodedState,
30+
getEncodedState: () => encodedState,
3131
handleDecodeError,
32-
setLocationHash,
32+
onEncodedState: onEncodedState,
3333
}),
3434
);
3535

@@ -48,17 +48,20 @@ describe("useUrlState", () => {
4848
${"next state is function"} | ${jest.fn(() => emptyUrlStateDecoded)}
4949
`("sets location hash to encoded state value", ({ nextStateAction }) => {
5050
const { result } = renderHook(() =>
51-
useUrlState({ getLocationHash: () => urlStateEncoded, setLocationHash }),
51+
useUrlState({
52+
getEncodedState: () => urlStateEncoded,
53+
onEncodedState: onEncodedState,
54+
}),
5255
);
5356
const [, setState] = result.current;
5457

55-
expect(setLocationHash).not.toHaveBeenCalled();
58+
expect(onEncodedState).not.toHaveBeenCalled();
5659
setState(nextStateAction);
5760
if (typeof nextStateAction === "function") {
5861
expect(nextStateAction).toHaveBeenCalledTimes(1);
5962
expect(nextStateAction).toHaveBeenLastCalledWith(urlStateDecoded);
6063
}
61-
expect(setLocationHash).toHaveBeenCalledTimes(1);
62-
expect(setLocationHash).toHaveBeenLastCalledWith(emptyUrlStateEncoded);
64+
expect(onEncodedState).toHaveBeenCalledTimes(1);
65+
expect(onEncodedState).toHaveBeenLastCalledWith(emptyUrlStateEncoded);
6366
});
6467
});

src/hooks/useUrlState.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ export type UseUrlStateResult<T> = [
88
];
99

1010
export function useUrlState<T>(props: {
11-
setLocationHash(urlEncodedState: string): void;
12-
getLocationHash(): string;
11+
onEncodedState(urlEncodedState: string): void;
12+
getEncodedState(): string;
1313
handleDecodeError?(urlEncodedState: string): T;
1414
}): UseUrlStateResult<T> {
15-
const { getLocationHash, handleDecodeError, setLocationHash } = props;
15+
const { getEncodedState, handleDecodeError, onEncodedState } = props;
1616

1717
const state = useRef<T | null>(null);
1818
state.current = useMemo(() => {
19-
const encodedState = getLocationHash();
19+
const encodedState = getEncodedState();
2020
try {
2121
return urlDecode<T>(encodedState);
2222
} catch (e) {
2323
return handleDecodeError?.(encodedState) ?? null;
2424
}
25-
}, [getLocationHash, handleDecodeError]);
25+
}, [getEncodedState, handleDecodeError]);
2626

2727
const setState: UseUrlStateResult<T>[1] = useCallback(
2828
function setState(nextStateAction) {
@@ -31,9 +31,9 @@ export function useUrlState<T>(props: {
3131
? (nextStateAction as CallableFunction)(state.current)
3232
: nextStateAction;
3333
const hash = urlEncode(newState);
34-
setLocationHash(hash);
34+
onEncodedState(hash);
3535
},
36-
[setLocationHash],
36+
[onEncodedState],
3737
);
3838

3939
return [state.current, setState];

0 commit comments

Comments
 (0)