英文:
React.js Pure and impure components
问题
- 为什么这个
Cup
组件运行两次? - 即使它运行两次,为什么它不打印其他数字(1、3 和 5)?
- 当我在
TeaSet
组件中打印guest
变量时,无论我在哪里打印它,它都显示为 0,为什么会这样?
英文:
So, I am learning react.js and was looking at the documentation where they have given a code for impure component
let guest = 0;
function Cup() {
// Bad: changing a preexisting variable!
guest = guest + 1;
return <h2>Tea cup for guest #{guest}</h2>;
}
export default function TeaSet() {
return (
<>
<Cup />
<Cup />
<Cup />
</>
);
}
and this is the output of the above code:
I don't understand why this is the output. After adding seeing the log output I saw that the Cup
component is running twice. So, my questions are:
- Why does this
Cup
component run twice? - Even if it runs twice, why does it not print the other numbers (1, 3 and 5)?
- Also when I print the
guest
variable in theTeaSet
component it says thatguest
is 0 no matter where I print it, why is this?
答案1
得分: 3
Nice to hear that you're learning React. AS your Queries let me clear a few basic funda i.e.
=> React runs with 2 virtual Doms and due to that the function will be called Twice. For that either you may disable "Strict Mode" from index.js.
But that is not a better way, So instead of that you may use UseEffect Hook.
function Cup() {
let [guest,setGuest] = useState(0);
useEffect(()=>{
setGuest(guest + 1);
,[])
return <h2>Tea cup for guest #{guest}</h2>;
}
For the Detailed information about UseEffect Hook Click Here
I hope your queries wil be solved using this, All the Best.
And As you're expecting to render in TeaPost Component, It can't be rendered with different values by the mentioned snippeas Changes will be applied Globally.
英文:
Nice to hear that you're learning React. AS your Queries let me clear a few basic funda i.e.
=> React runs with 2 virtual Doms and due to that the function will be called Twice. For that either you may disable "Strict Mode" from index.js.
But that is not a better way, So instead of that you may use UseEffect Hook.
function Cup() {
let [guest,setGuest] = useState(0);
useEffect(()=>{
setGuest(guest + 1);
,[])
return <h2>Tea cup for guest #{guest}</h2>;
}
For the Detailed information about UseEffect Hook Click Here
I hope your queries wil be solved using this, All the Best.
And As you're expecting to render in TeaPost Component, It can't be rendered with different values by the mentioned snippeas Changes will be applied Globally.
答案2
得分: 1
-
如果您使用
<StrictMode/>
组件,React 将在严格模式下运行,以帮助您找到意外的不纯代码。 -
React 在“commit 阶段”而不是“render 阶段”更新 DOM。
-
React 组件树中渲染阶段的顺序是:父组件 => 子组件。这意味着父组件
TeaSet
的渲染函数将首先被调用(此时guest
变量的值为0
),然后是子组件Cup
。
参见 https://stackoverflow.com/a/76218354/6463558 和 在开发中修复双重渲染导致的错误。
英文:
- React runs in a strict mode if you use the
<StrictMode/>
component to help you find accidentally impure code.
> Strict Mode always calls your rendering function twice
-
React update the DOM in "commit phrase", not "render phrase"
-
The order of the render phrase in the React component tree is: parent => child. It means the render function of the parent component
TeaSet
will be called first(at this time the value ofguest
variable is0
), then the child componentCup
.
See https://stackoverflow.com/a/76218354/6463558 and Fixing bugs found by double rendering in development
The
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论