React.js纯组件和非纯组件

huangapple go评论69阅读模式
英文:

React.js Pure and impure components

问题

  1. 为什么这个 Cup 组件运行两次?
  2. 即使它运行两次,为什么它不打印其他数字(1、3 和 5)?
  3. 当我在 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:

React.js纯组件和非纯组件

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:

  1. Why does this Cup component run twice?
  2. Even if it runs twice, why does it not print the other numbers (1, 3 and 5)?
  3. Also when I print the guest variable in the TeaSet component it says that guest 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(()=&gt;{
  setGuest(guest + 1);
  ,[])
  return &lt;h2&gt;Tea cup for guest #{guest}&lt;/h2&gt;;
}

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

  1. 如果您使用<StrictMode/>组件,React 将在严格模式下运行,以帮助您找到意外的不纯代码。

  2. React 在“commit 阶段”而不是“render 阶段”更新 DOM。

  3. React 组件树中渲染阶段的顺序是:父组件 => 子组件。这意味着父组件 TeaSet 的渲染函数将首先被调用(此时 guest 变量的值为 0),然后是子组件 Cup

参见 https://stackoverflow.com/a/76218354/6463558在开发中修复双重渲染导致的错误

英文:
  1. React runs in a strict mode if you use the &lt;StrictMode/&gt; component to help you find accidentally impure code.

> Strict Mode always calls your rendering function twice

  1. React update the DOM in "commit phrase", not "render phrase"

  2. 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 of guest variable is 0), then the child component Cup.

See https://stackoverflow.com/a/76218354/6463558 and Fixing bugs found by double rendering in development

The

huangapple
  • 本文由 发表于 2023年6月12日 12:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定