[英]useState not updating directly in reactjs
I have some div
element and only header is revealed, when I click on it, it should display the form and if the form is displayed it should close it.我有一些
div
元素,只有 header 显示,当我点击它时,它应该显示表单,如果显示表单,它应该关闭它。
I tried this code, but it's not working, probably because useState
is async so it's not updating right away.我试过这段代码,但它不起作用,可能是因为
useState
是异步的,所以它没有立即更新。
I have tried using useEffect
but my implementation wasn't succesful, here's a code snippet:我曾尝试使用
useEffect
但我的实现不成功,这是一个代码片段:
import React, { useState } from 'react';
import './AdminPageStyle.css';
const AdminPage = () => {
document.title = 'Admin page';
const [addPanel, setAddPanel] = useState(false);
return(
<main className="main_class">
<h1>Hello admin!</h1>
{/* Navbar */}
<nav className="database-manipualtion_class">
<div className="add-update-delete_class" id="add-panel_id">Add new animation</div>
<div className="add-update-delete_class" id="update-panel_id">Update existing animation</div>
<div className="add-update-delete_class" id="delete-panel_id">Delete animation</div>
</nav>
{/* Panel for adding new animation */}
<div className="add_class" onClick={() => setAddPanel(!addPanel)} style={{display: addPanel ? 'flex': 'none'}}>
<h3>Add new animation</h3>
<form className="form_class">
<div className="input-field_class">
<label className="label_class" htmlFor="add-name">Add name</label>
<input type="text" name="add-name" id="add-name_id" placeholder="Awesome animation" />
</div>
<div className="input-field_class">
<label className="label_class" htmlFor="add-gifAdrress">Add gif adrress</label>
<input type="text" name="add-gifAdrress" id="add-gifAdrress_id" placeholder="/images/animation_gifs/loader.png" />
</div>
<button type="button" className="add-new-animation_class">Make new animation</button>
</form>
</div>
The problem was I was trying to display/hide the header and not the form, so this:问题是我试图显示/隐藏 header 而不是表单,所以这是:
<nav className="database-manipualtion_class">
<div className="add-update-delete_class" id="add-panel_id">Add new animation</div>
<div className="add-update-delete_class" id="update-panel_id">Update existing animation</div>
<div className="add-update-delete_class" id="delete-panel_id">Delete animation</div>
</nav>
{/* Panel for adding new animation */}
<div className="add_class" onClick={() => setAddPanel(!addPanel)} style={{display: addPanel ? 'flex': 'none'}}>
Should be translated to this:应该翻译成这样:
<nav className="database-manipualtion_class">
<div onClick={() => setAddPanel(!addPanel)} className="add-update-delete_class" id="add-panel_id">Add new animation</div>
<div className="add-update-delete_class" id="update-panel_id">Update existing animation</div>
<div className="add-update-delete_class" id="delete-panel_id">Delete animation</div>
</nav>
{/* Panel for adding new animation */}
<div className="add_class" style={{display: addPanel ? 'flex': 'none'}}>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.