CodinGame(React 测试):我不明白答案
CodinGame (React test) : I don't understand the answer
这是 CodinGame 中 React 测试的演示:
"From the following code snippet, what will be the color of "Hello world!"(第 11 行)在按下 "Change color" 按钮后?"
https://www.codingame.com/assessment/fileservlet?id=16114156909252
一个。红色的
b.绿色
C。此代码将引发错误
d.默认颜色取决于浏览器
答案是a。红色,但我不明白为什么它不是 b。绿色。
有什么想法吗?
提前致谢!
您必须 error.As 您的 parent class
return 方法是
render(){
return()//here is the error
<div>
...
</div>)
}
应该是
render(){
return(
<div>
...
</div>)
}
这可以给出正确的结果或将您的完整代码作为文本上传,以便我们查看和测试。
同意 Rajan 的观点,但我认为这个测验更关注的是,使用来自 parents
的道具分配本地状态是一种非常糟糕的做法
this.state = { color: props.color }
为什么?因为这个本地状态永远不会用新的道具更新。事实上,构造函数只被调用一次,本地状态是用颜色属性值设置的,永远不会改变。
这是 CodinGame 中 React 测试的演示:
"From the following code snippet, what will be the color of "Hello world!"(第 11 行)在按下 "Change color" 按钮后?"
https://www.codingame.com/assessment/fileservlet?id=16114156909252
一个。红色的 b.绿色 C。此代码将引发错误 d.默认颜色取决于浏览器
答案是a。红色,但我不明白为什么它不是 b。绿色。 有什么想法吗?
提前致谢!
您必须 error.As 您的 parent class return 方法是
render(){
return()//here is the error
<div>
...
</div>)
}
应该是
render(){
return(
<div>
...
</div>)
}
这可以给出正确的结果或将您的完整代码作为文本上传,以便我们查看和测试。
同意 Rajan 的观点,但我认为这个测验更关注的是,使用来自 parents
的道具分配本地状态是一种非常糟糕的做法this.state = { color: props.color }
为什么?因为这个本地状态永远不会用新的道具更新。事实上,构造函数只被调用一次,本地状态是用颜色属性值设置的,永远不会改变。