React的切换如何工作

发布于 2025-02-12 08:24:33 字数 681 浏览 5 评论 0原文

以下是一个简单的反应切换功能。但是我很困惑。在第一次点击中,我了解“ setState”函数使“状态”到“ true”,因此内容显示。但是在第二次点击中,“ setstate”已经是正确的。然后,错误状态如何工作。

  1. 首先单击按钮
  2. “变化”函数使“状态”对“ true”
  3. 状态'为“ true”,因此显示内容为
  4. 触发priggt pick phick pick picke theck the sight
  5. 会发生什么?

任何人都可以简单地解释它。提前致谢!

import React, {useState} from 'react'

function App() {

  const [state,setState]=useState(false);
  console.log(state);
  const changeState=()=>{
    setState(!state);
  }
  return (
    <>
    {state ? <div>Show Content</div> : null}
    <button onClick={changeState}>Toggle</button>
    </>
  )
}

export default App

Following is a simple React Toggle functionality. But I have a confusion. In the first click, I understand that the 'setState' function makes the 'state' to 'true' so the content shows. But in the second click 'setState' is already true. Then how the false state working.

  1. First Click on the button
  2. 'changeState' function makes 'state' to 'true'
  3. 'state' is 'true' so the content is shows
  4. Trigger the second click
  5. What will happend ?

Can any one explain it in simple way. Thanks in advance!

import React, {useState} from 'react'

function App() {

  const [state,setState]=useState(false);
  console.log(state);
  const changeState=()=>{
    setState(!state);
  }
  return (
    <>
    {state ? <div>Show Content</div> : null}
    <button onClick={changeState}>Toggle</button>
    </>
  )
}

export default App

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪之触 2025-02-19 08:24:33

状态的初始值是false。因此,首次单击后,您将state设置为state,这使其成为true(因为!false代码>等于true)。在第二次单击中,它将变成false,因为statetrue,并将其设置为!状态使其<代码> false (!true等于false)。感叹号()被读取为不是

顺便说一句,我建议设置这样的状态:

setState(prev => !prev);

希望这会有所帮助。

The initial value of state is false. So, after you do the first click, you set state to !state, which makes it true (because !false equals true). On the second click, it will become false because state was true and setting it to !state made it false (!true equals false). The exclamation mark (!) is read as not.

By the way, I recommend setting the state like this:

setState(prev => !prev);

Hope this helps.

预谋 2025-02-19 08:24:33

在setState并希望将上一个值作为输入时,您应该这样做:

setState(pre => !pre)

在这里,我们通过在USESTATE函数中使用Pre获得当前值。这应该很好地工作。

In react when you setState and want to use the previous value as the input you should do this:

setState(pre => !pre)

Here we get the current value by using pre in the useState function. This should work perfectly.

生生漫 2025-02-19 08:24:33

const usetoggle =(初始状态)=&gt; {
const [toggleValue,settoggleValue] = usestate(initialstate);

const toggler = () => { setToggleValue(!toggleValue) };

};

const useToggle = (initialState) => {
const [toggleValue, setToggleValue] = useState(initialState);

const toggler = () => { setToggleValue(!toggleValue) };

};

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文