React setState()返回什么? [英] what does React setState() return?

查看:234
本文介绍了React setState()返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在三元组中使用 this.setState()并试图找出它返回的内容。该文档没有提供任何返回信息。

I am using this.setState()inside of a ternary and trying to figure out what it returns. The documentation doesn't provide any information as to what it returns.

推荐答案

如前所述,


我认为它不会返回任何东西。它是一个二传手。

I dont think it returns anything. Its a setter.

你可以参考 setState docs 以了解它的作用。

You can refer setState docs to understand what it does.

现在回答你的问题,因为react与Typescript密切相关,以下代码来自 index.d.ts 反应文件。你可以找到这里的文件,你可以检查定义此处

Now to answer your question, since react is closely associated with Typescript, following code is from the index.d.ts file of react. You can find the file here and you can check the definition here

setState<K extends keyof S>(f: (prevState: Readonly<S>, props: P) => Pick<S, K>, callback?: () => any): void;
setState<K extends keyof S>(state: Pick<S, K>, callback?: () => any): void;

如您所见,这两个定义具有相同的返回类型:空隙。这是因为,首先,它是一个setter函数。其次,在大多数情况下,setState将触发重新渲染。所以你将无法做很多事情。

As you see, both the definitions have same return type: void. This is because, first, it is a setter function. Second, on most occasion, setState will trigger a re-render. So you will not be able to do much.

如果你必须在setState之后进行一些处理,你可以将回调作为第二个参数传递。

If you have to do some processing after setState, you can pass a callback as second argument.

这篇关于React setState()返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆