React 中的 useState() 是什么?

React 中的 useState() 是什么?,第1张

我目前正在学习 React 中的钩子概念并试图理解下面的示例。

import { useState } from 'react';function Example() {    // Declare a new state variable, which we'll call "count"    const [count, setCount] = useState(0);  return (    <div>      <p>You clicked {count} times</p>      <button onClick={() => setCount(count + 1)}>        Click me      </button>    </div>  );}

上面的例子增加了处理函数参数本身的计数器。如果我想在事件处理函数中修改计数值怎么办

考虑下面的例子:

setCount = () => {  //how can I modify count value here. Not sure if I can use setState to modify its value  //also I want to modify other state values as well here. How can I do that}<button onClick={() => setCount()}>  Click me</button>

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/read/1369984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-15
下一篇 2022-06-15

发表评论

登录后才能评论
保存