- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
据我所知,componentWillMount
唯一可以做而 constructor
不能做的就是调用 setState
。
componentWillMount() {
setState({ isLoaded: false });
}
由于我们还没有调用render
,componentWillMount
中的setState
将在我们进入第一个render 之前准备状态对象()
通过。这与 constructor
所做的本质上是一样的:
constructor(props) {
super(props);
this.state = { isLoaded: false };
}
但我看到另一个用例,其中 componentWillMount
很有用(在服务器端)。
让我们考虑一些异步的东西:
componentWillMount() {
myAsyncMethod(params, (result) => {
this.setState({ data: result });
})
}
这里我们不能使用 constructor
因为赋值给 this.state
不会触发 render()
。
componentWillMount
中的setState
呢?根据React docs :
componentWillMount()
is invoked immediately before mounting occurs. It is called beforerender(
), therefore setting state in this method will not trigger a re-rendering. Avoid introducing any side-effects or subscriptions in this method.
因此,在这里我认为 React 将在第一次渲染时使用新的状态值并避免重新渲染。
问题 1: 这是否意味着,在 componentWillMount
中,如果我们在异步方法的回调中调用 setState
(可以是 promise 回调) , React 阻塞初始渲染直到执行回调?
在客户端 进行此设置(是的,我在服务器端渲染中看到了该用例),如果我假设上述情况属实,则在我的异步方法完成之前我将看不到任何内容。
我是否遗漏了任何概念?
问题 2: 我可以仅使用 componentWillMount
而不是使用 constructor
和 来实现的任何其他用例componentDidMount
?
最佳答案
Does this means, inside componentWillMount, if we call setState in an async method's callback (can be a promise callback), React blocks initial rendering until the callback is executed?
不,见 here .
以下代码不会阻止渲染(请记住,无论如何调用 setState 都是一种反模式)
componentWillMount: function() {
new Promise((resolve, reject) => {
setTimeout(()=> {
resolve();
}, 2000)
}).then(() => this.setState({ promiseResult: 'World' }));
},
Question 2: Are the any other use cases that I can achieve with componentWillMount only, but not using the constructor and componentDidMount?
不,对于 ES6 类,您可以丢弃 componentWillMount。仅当您使用 React.createClass({... })
编辑:显然,我错了。感谢@Swapnil指出这一点。这是 discussion .
如果 constructor
中存在修改另一个组件状态的副作用,React 会抛出警告,因为它假设 constructor
中的 setState
code> 本身以及可能在调用 render()
期间。因此,构造函数
中不需要任何副作用。
如果你在 componentWillMount
中这样做,情况就不是这样了,不会抛出任何错误。另一方面,来自 facebook 的人也不鼓励 componentWillMount
中的副作用。因此,如果您没有任何副作用,则可以使用 constructor
而不是 componentWillMount
。对于副作用,建议使用 componentDidMount
而不是 componentWillMount
。无论哪种方式,您都不需要 componentWillMount
。
关于javascript - 构造函数 vs componentWillMount;什么是 componentWillMount 可以做而构造函数不能做的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40828004/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!