简体   繁体   English

React hook useState 未更新 onclick

[英]React hook useState not updating onclick

I am trying to update the sidebar class when the closehandler function is called but the state is not updating.当调用closehandler function 但 state 未更新时,我正在尝试更新侧边栏 class。 How do I go about that.我该怎么做?

function RiderSidebar({ sidebar, close }) {

const [SidebarClass, setSidebarClass] = useState('sidebar')

const closeHandler = (e) => {

    e.preventDefault();
    console.log("closed click");
    setSidebarClass("user-side-bar close")
    console.log("slidebar is " + SidebarClass);

    setTimeout(() => {
        close()
    }, 1000)
}
return (
    <div className={SidebarClass} >
        <button className="close-btn" onClick={closeHandler}>X</button>

        <ul>
            <li><NavLink to='/rider/dashboard' className="user-nav"  ><DashboardIcon />  Dashboard</NavLink></li>
            <li><NavLink to='/rider/orders' className="user-nav"  ><HistoryIcon /> Orders</NavLink></li>
            <li><NavLink to='/user/logout' className="user-nav"  ><PowerSettingsNewIcon /> Logout</NavLink></li>
        </ul>
    </div >
)
}

React batches the state update. React 批处理 state 更新。 If you will check the state update immediately after updating the state react then you may not get the updated state.如果您在更新 state 反应后立即检查 state 更新,那么您可能无法获得更新的 state。 You can check the updated state in render method.您可以在渲染方法中查看更新的 state。 It should be updated there.它应该在那里更新。

useState hook is asynchronous, and will not be reflected immediately.. try this: useState 钩子是异步的,不会立即反映..试试这个:

  useEffect(() => {
    
    }, [SidebarClass]);

I think you add useEffect hooks to check I explain briefly what did you wrong when you click your close button closed handler function trigger and setState function calling at the same time( asynchronous ) console.log statement printed.我认为您添加useEffect挂钩来检查我简要解释了当您单击关闭按钮关闭处理程序 function 触发器和setState function 同时调用(异步console.log语句时您做错了什么。 //'side bar'

in your code, you can't verify your state change or not (only you can verify from the render method) because the closed handler function triggers only one time if you want to check your className is changed or not from console.log statement you must need useEffect hook it will trigger every time after your state changed so can conform it your className is changed or not在您的代码中,您无法验证您的 state 更改与否(只有您可以从渲染方法验证),因为如果您想检查您的className是否从console.log语句中更改,关闭的处理程序 function 只会触发一次必须需要useEffect挂钩,它会在您的state更改后每次触发,因此可以符合您的className是否更改

//only trigger one time 
 useEffect(()=>{
 console.log(SidebarClass);//output 'sidebar'
 },[])
        
 //hooks trigger every time after your state changed
  useEffect(()=>{
 console.log(SidebarClass); 
 //output 'sidebar' 
 //output user-side-bar close
            })
        
 //only trigger if SidebarClass state changed
 useEffect(()=>{
 console.log(SidebarClass);
 //output 'sidebar' 
 //output user-side-bar close
 },[SidebarClass]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM