Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add param to allow dynamic key #612

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/useOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ type BaseUseOnyxOptions = {
* with the same connect configurations.
*/
reuseConnection?: boolean;

/**
* If set to `true`, the key can be changed dynamically during the component lifecycle.
*/
allowDynamicKey?: boolean;
};

type UseOnyxInitialValueOption<TInitialValue> = {
Expand Down Expand Up @@ -157,7 +162,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(

useEffect(() => {
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
if (previousKey === key) {
if (options?.allowDynamicKey || previousKey === key) {
return;
}

Expand All @@ -181,7 +186,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
throw new Error(
`'${previousKey}' key can't be changed to '${key}'. useOnyx() only supports dynamic keys if they are both collection member keys from the same collection e.g. from 'collection_id1' to 'collection_id2'.`,
);
}, [previousKey, key]);
}, [previousKey, key, options?.allowDynamicKey]);

useEffect(() => {
// This effect will only run if the `dependencies` array changes. If it changes it will force the hook
Expand Down Expand Up @@ -342,4 +347,4 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(

export default useOnyx;

export type {FetchStatus, ResultMetadata, UseOnyxResult};
export type {FetchStatus, ResultMetadata, UseOnyxResult, BaseUseOnyxOptions, UseOnyxSelector, UseOnyxSelectorOption, UseOnyxInitialValueOption, UseOnyxOptions};
38 changes: 38 additions & 0 deletions tests/unit/useOnyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,44 @@ describe('useOnyx', () => {
fail("Expected to don't throw any errors.");
}
});

it('should not throw an error when changing from a non-collection key to another one if allowDynamicKey is true', async () => {
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: true}), {initialProps: ONYXKEYS.TEST_KEY});

try {
await act(async () => {
rerender(ONYXKEYS.TEST_KEY_2);
});
} catch (e) {
fail("Expected to don't throw any errors.");
}
});

it('should throw an error when changing from a non-collection key to another one if allowDynamicKey is false', async () => {
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: false}), {initialProps: ONYXKEYS.TEST_KEY});

try {
await act(async () => {
rerender(ONYXKEYS.TEST_KEY_2);
});

fail('Expected to throw an error.');
} catch (e) {
expect((e as Error).message).toBe(error(ONYXKEYS.TEST_KEY, ONYXKEYS.TEST_KEY_2));
}
});

it('should not throw an error when changing from a collection member key to another one if allowDynamicKey is true', async () => {
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: true}), {initialProps: `${ONYXKEYS.COLLECTION.TEST_KEY}` as string});

try {
await act(async () => {
rerender(`${ONYXKEYS.COLLECTION.TEST_KEY_2}`);
});
} catch (e) {
fail("Expected to don't throw any errors.");
}
});
});

describe('misc', () => {
Expand Down
Loading