Skip to content

Commit 43243d8

Browse files
authored
Merge pull request #612 from wildan-m/wildan/fix/51296-make-onyx-allow-dynamic-key
Add param to allow dynamic key
2 parents 10831d6 + f2cc869 commit 43243d8

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

lib/useOnyx.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type BaseUseOnyxOptions = {
3232
* with the same connect configurations.
3333
*/
3434
reuseConnection?: boolean;
35+
36+
/**
37+
* If set to `true`, the key can be changed dynamically during the component lifecycle.
38+
*/
39+
allowDynamicKey?: boolean;
3540
};
3641

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

158163
useEffect(() => {
159164
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
160-
if (previousKey === key) {
165+
if (options?.allowDynamicKey || previousKey === key) {
161166
return;
162167
}
163168

@@ -181,7 +186,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
181186
throw new Error(
182187
`'${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'.`,
183188
);
184-
}, [previousKey, key]);
189+
}, [previousKey, key, options?.allowDynamicKey]);
185190

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

343348
export default useOnyx;
344349

345-
export type {FetchStatus, ResultMetadata, UseOnyxResult};
350+
export type {FetchStatus, ResultMetadata, UseOnyxResult, BaseUseOnyxOptions, UseOnyxSelector, UseOnyxSelectorOption, UseOnyxInitialValueOption, UseOnyxOptions};

tests/unit/useOnyxTest.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,44 @@ describe('useOnyx', () => {
8989
fail("Expected to don't throw any errors.");
9090
}
9191
});
92+
93+
it('should not throw an error when changing from a non-collection key to another one if allowDynamicKey is true', async () => {
94+
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: true}), {initialProps: ONYXKEYS.TEST_KEY});
95+
96+
try {
97+
await act(async () => {
98+
rerender(ONYXKEYS.TEST_KEY_2);
99+
});
100+
} catch (e) {
101+
fail("Expected to don't throw any errors.");
102+
}
103+
});
104+
105+
it('should throw an error when changing from a non-collection key to another one if allowDynamicKey is false', async () => {
106+
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: false}), {initialProps: ONYXKEYS.TEST_KEY});
107+
108+
try {
109+
await act(async () => {
110+
rerender(ONYXKEYS.TEST_KEY_2);
111+
});
112+
113+
fail('Expected to throw an error.');
114+
} catch (e) {
115+
expect((e as Error).message).toBe(error(ONYXKEYS.TEST_KEY, ONYXKEYS.TEST_KEY_2));
116+
}
117+
});
118+
119+
it('should not throw an error when changing from a collection member key to another one if allowDynamicKey is true', async () => {
120+
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: true}), {initialProps: `${ONYXKEYS.COLLECTION.TEST_KEY}` as string});
121+
122+
try {
123+
await act(async () => {
124+
rerender(`${ONYXKEYS.COLLECTION.TEST_KEY_2}`);
125+
});
126+
} catch (e) {
127+
fail("Expected to don't throw any errors.");
128+
}
129+
});
92130
});
93131

94132
describe('misc', () => {

0 commit comments

Comments
 (0)