5

看到一段代码, setTimeout(resolve, 0), 不是很明白用意?

 3 years ago
source link: https://www.v2ex.com/t/799308
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  React

看到一段代码, setTimeout(resolve, 0), 不是很明白用意?

  yazoox · 47 分钟前 · 323 次点击
const whenStable = async () =>
  await act(async () => {
    await new Promise((resolve) => setTimeout(resolve, 0));
  });

原帖地址: https://stackoverflow.com/questions/60137762/how-can-i-test-a-react-hooks-component-by-changing-usestate?rq=1 只有一个回复,回复里面的测试文件,里面用到了这个

不是很理解这个的用意。如果说需要延时,那为什么 setTimeout 的参数是 0? 还是说,作者只是举个例子?应该设为比如 500,etc.?

以及,这个貌似有 warning

Warning: The callback passed to ReactTestUtils.act(...) function must not return anything.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK