它似乎componentWillReceiveProps
将在即将发布的版本中完全淘汰,支持新的生命周期方法getDerivedStateFromProps
.
https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops
经过检查,看起来你现在无法在this.props
和nextProps
你之间进行直接比较componentWillReceiveProps
.有没有办法解决?
此外,它现在返回一个对象.我是否正确地假设返回值基本上是this.setState
?
以下是我在网上找到的一个例子 https://github.com/reactjs/rfcs/blob/master/text/0006-static-lifecycle-methods.md#state-derived-from-propsstate
之前
class ExampleComponent extends React.Component {
state = {
derivedData: computeDerivedState(this.props)
};
componentWillReceiveProps(nextProps) {
if (this.props.someValue !== nextProps.someValue) {
this.setState({
derivedData: computeDerivedState(nextProps)
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
后
class ExampleComponent extends React.Component {
// Initialize state in constructor,
// Or with a property initializer.
state = {};
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.someMirroredValue !== nextProps.someValue) {
return {
derivedData: computeDerivedState(nextProps),
someMirroredValue: nextProps.someValue
}; …
Run Code Online (Sandbox Code Playgroud) 我遇到一个问题,我的特定输入会产生错误.
library(kriging)
x <- c(0.65,0.45,0.25,0.65,0.45,0.25,0.55,0.4,0.25,0.55,0.4,0.25,0.6,0.45,0.25,0.6,0.45,0.25,0.5,0.4,0.25,0.5,0.4,0.25,0.55,0.4,0.25,0.55,0.4,0.25,0.5,0.35,0.25,0.5,0.35,0.25)
y <- c(0.25,0.45,0.65,0.2,0.4,0.6,0.25,0.4,0.55,0.2,0.35,0.5,0.25,0.4,0.6,0.2,0.35,0.55,0.25,0.35,0.5,0.2,0.3,0.45,0.25,0.4,0.55,0.2,0.35,0.5,0.2,0.35,0.45,0.15,0.3,0.4)
r <- c(241.5,236.8333333,229.875,242,235.5,231.3333333,238,236.875,225.75,238.5,233.25,228.5,24,0.5,237.1666667,229.5,241.3333333,236.8333333,227.75,237.625,233.7,228.3333333,236.8,235,229.8333333,238.1,234.6,228.6666667,237.375,235.1,228.6666667,236.4,231.6666667,227.3,236.5,232.625,227.3571429)
x <- data.matrix(x)
x <- sweep(x,1,100,"*")
y <- data.matrix(y)
y <- sweep(y,1,100,"*")
krig <- kriging(x,y,r,lag=3)
Error in solve.default(matrix(A, n + 1, n + 1)) :Lapack routine dgesv: system is exactly singular: U[26,26] = 0
Run Code Online (Sandbox Code Playgroud)
老实说我不知道出了什么问题.我的格式似乎与我见过的示例代码完全一致.
我想重新生成一个生成data.frames的循环.这是我正在寻找的一般想法的[错误]循环.
for (i in 1:2){
a <- c(2, (i+10))
b <- c((i+10)), 5))
c[i] <- data.frame(a,b)
}
rbind(c)
Run Code Online (Sandbox Code Playgroud)
我想要一个像这样的输出:
2 11
11 5
2 12
12 5
Run Code Online (Sandbox Code Playgroud)
之前已经问过这个问题,但答案是一个直接的解决方案,没有任何解释.我不知道怎么读.它涉及do.call
和mget
.