English 中文(简体)
(单位:千美元) + 2
原标题:() => count + 2 vs. (count) => count + 2
function Counter() {
  const [count, setCount] = useState(0);
  const [calculation, setCalculation] = useState(0);

  useEffect(() => {
    setCalculation(() => count * 2);
  }, [count]); // <- add the count variable here

  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount((c) => c + 1)}>+</button>
      <p>Calculation: {calculation}</p>
    </>
  );
}

我对这一职能的不理解是:

setCalculation(() => count * 2); setCount((c) => c + 1);

They do the similar work, but why does the first line NOT have the parameter? Does this make any difference between two lines?

为什么使用“c”而不是“count”? (While `count'在前线使用)。

谢谢。

我认为他们也做了同样的工作,但我现在并不肯定。

问题回答

两者在计算现值时有微妙差异。

页: 1

setState(() => count*2)
// or
setState(count * 2)

<代码>>值从现在起算为关闭。 主要含义是,将<代码>(<>count>/code>值用于更新国家并不保证您具备最新数据。

如果设定者可能无法按规定行事,例如因子业务、同时交付等。

差异的最简单表现是,共同发出多重呼吁:

// Assume count is initially 5
setState(count * 2) // count is 10
setState(count * 2) // count is _still_ 10, because the `count` variable is still 5

Versus:

// Assume count is initially 5
setState(count => count * 2) // count is 10
setState(count => count * 2) // count is 20

反馈格式保证了你获得最新价值。

作为一项建议,如果你的新国家取决于老国家,就可以通过背靠背。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签