Skip to content

Commit d89ec79

Browse files
committed
chore: useState ignoreDestroy is optional
1 parent 96e03ef commit d89ec79

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

src/hooks/useState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function useState<T>(
2929
[],
3030
);
3131

32-
function safeSetState(updater: Updater<T>, ignoreDestroy: boolean) {
32+
function safeSetState(updater: Updater<T>, ignoreDestroy?: boolean) {
3333
if (ignoreDestroy && destroyRef.current) {
3434
return;
3535
}

tests/hooks.test.js

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,59 @@ describe('hooks', () => {
118118
});
119119
});
120120

121-
it('useState', done => {
122-
const errorSpy = jest.spyOn(console, 'error');
123-
124-
const Demo = () => {
125-
const [val, setValue] = useState(0);
126-
127-
React.useEffect(
128-
() => () => {
129-
setTimeout(() => {
130-
setValue(1, true);
131-
}, 0);
132-
},
133-
[],
134-
);
121+
describe('useState', () => {
122+
it('not throw', done => {
123+
const errorSpy = jest.spyOn(console, 'error');
124+
125+
const Demo = () => {
126+
const [val, setValue] = useState(0);
127+
128+
React.useEffect(
129+
() => () => {
130+
setTimeout(() => {
131+
setValue(1, true);
132+
}, 0);
133+
},
134+
[],
135+
);
136+
137+
return null;
138+
};
135139

136-
return null;
137-
};
140+
const wrapper = mount(<Demo />);
141+
wrapper.unmount();
138142

139-
const wrapper = mount(<Demo />);
140-
wrapper.unmount();
143+
setTimeout(() => {
144+
expect(errorSpy).not.toHaveBeenCalled();
145+
done();
146+
}, 50);
147+
});
141148

142-
setTimeout(() => {
143-
expect(errorSpy).not.toHaveBeenCalled();
144-
done();
145-
}, 50);
149+
it('throw', done => {
150+
const errorSpy = jest.spyOn(console, 'error');
151+
152+
const Demo = () => {
153+
const [val, setValue] = useState(0);
154+
155+
React.useEffect(
156+
() => () => {
157+
setTimeout(() => {
158+
setValue(1);
159+
}, 0);
160+
},
161+
[],
162+
);
163+
164+
return null;
165+
};
166+
167+
const wrapper = mount(<Demo />);
168+
wrapper.unmount();
169+
170+
setTimeout(() => {
171+
expect(errorSpy).toHaveBeenCalled();
172+
done();
173+
}, 50);
174+
});
146175
});
147176
});

0 commit comments

Comments
 (0)