Skip to content

Commit 21d20ad

Browse files
authored
feat: make dependency list optional (#131)
The dependency list is optional in the React useEffect hook
1 parent 819f9ca commit 21d20ad

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ function useAsyncEffect(
278278
) => void,
279279
cast: <T>(promise: Promise<T>) => Generator<Promise<T>, T>
280280
) => Iterator<any, any, any>,
281-
deps: React.DependencyList
281+
deps?: React.DependencyList
282282
): void;
283283
```
284284

src/use-async-effect.spec.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ it("calls the generator again once a dependency changes", () => {
5050
expect(callable).toHaveBeenCalledTimes(2);
5151
});
5252

53+
54+
it("calls the generator on each render if no dependency list given", () => {
55+
const callable = jest.fn();
56+
57+
let setState = (() => undefined) as (str: string) => void;
58+
59+
const TestComponent: React.FC = () => {
60+
const [, _setState] = React.useState<string>("hello");
61+
useAsyncEffect(function* () {
62+
callable();
63+
});
64+
setState = _setState;
65+
return null;
66+
};
67+
68+
render(<TestComponent />);
69+
70+
act(() => {
71+
setState("aye");
72+
});
73+
act(() => {
74+
setState("bye");
75+
});
76+
77+
expect(callable).toHaveBeenCalledTimes(3);
78+
});
79+
5380
it("yield can resolve a non promise object", () => {
5481
const callable = jest.fn();
5582

src/use-async-effect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const useAsyncEffect = (
1919
) => void,
2020
cast: <T>(promise: Promise<T>) => Generator<Promise<T>, T>
2121
) => Iterator<unknown, GeneratorReturnValueType>,
22-
deps: React.DependencyList
22+
deps?: React.DependencyList
2323
): void => {
2424
const generatorRef = useRef(createGenerator);
2525

0 commit comments

Comments
 (0)