gpt4 book ai didi

reactjs - 为什么 React 生命周期方法返回 `mixed` 而不是 `void` ?

转载 作者:行者123 更新时间:2023-12-03 14:13:26 24 4
gpt4 key购买 nike

谈论此处列出的类型:https://github.com/facebook/flow/blob/v0.63.1/lib/react.js#L45-L66

他们不应该返回 void 因为他们不返回任何东西吗?

最佳答案

这是因为Flow issue #1803 "Allow react components' lifecycle methods to be async" 。这意味着应该允许返回一个 promise ,以便这些函数可以是异步的。

一个Flow TODO issue "Allow React lifecycle methods to return mixed"基于该讨论创建了,并且 this patch引入了 void 的更改至mixed .

推理(引用)

This will allow people to write stuff like:

class Component extends React.Component {
async componentDidMount() {
// ...
}
}

该问题可以通过返回类型 void | Promise<void> 来解决,但选择 mixed 的原因是

It doesn't really seem like the responsibility of Flow to enforce something that does not make a difference at runtime

关于reactjs - 为什么 React 生命周期方法返回 `mixed` 而不是 `void` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com