i have managed to toggle tables upon button clicks but i want for each table to only show when its button is clicked and then hide when another button meant for another table is clicked. where do i need to make changes and what do i need to add for it to work accordingly? This is the code im using currently:
const [show, toggleShow] = useState({});
const [showStock, toggleShowStock] = useState({});
const [showEcartStock,toggleShowEcartStock]=useState({});
<Layout>
<Content
style={{
margin: 0,
backgroundColor: "white",
}}
>
<div
className="site-layout-background"
style={{
padding: 24,
minHeight: 360,
}}
>
<Home sessionName={session_name} sessionID={state} session_status={session_status}/>
<div style={{ heigth: "80vh",margin: "16px" }}>
<Row
gutter={[13, 5]}
justify="center"
align="top"
>
<Col
onClick={() => toggleShowStock(!showStock)}>
<Button
style={{
backgroundColor: "#5e89af",
color: "white",
//padding: '18px 40px',
height: "60px",
width: "325px",
alignItems: "center",
}}
>
état stock
</Button>
</Col>
<Col onClick={() => toggleShow(!show)}>
<Button
style={{
backgroundColor: "#5e89af",
color: "white",
height: "60px",
width: "325px",
alignItems: "center",
}}
>
affectation
</Button>
</Col>
<Col
>
<Button
onClick={()=>{toggleShowEcartStock(!showEcartStock)}}
style={{
backgroundColor: "#5e89af",
color: "white",
height: "60px",
width: "325px",
alignItems: "center",
}}
>
écart stock
</Button>
</Col>
</Row>
</div>
{show && (
<AffectaionsTable sessionType={session_type} sessionName={session_name} sessionID={state} />
)}
{showStock && <StocksTable record={record} sessionID={state}/>}
{showEcartStock && <EcartStockRes sessionID={state}/>}
{showEcartStock && <EcartStockComTour sessionID={state}/>}
{showEcartStock && <EcartStockTechTour sessionID={state}/>}
</div>
</Content>
</Layout>
2
Answers
You can create an
onClick
function and setshow
andshowEcartStock
to false whenshowStock
is truehere is an eg:
Then use this function in
onClick
If the point is to make only one table visible at a time, you may slightly refactor the way you manage it with your state.
You may keep there only one
id
of the item being currently visible.Here’s a quick demo:
p.s. you may notice in above code snippet certain repeating pattern thus you may come up with the way to abstract that behind HOC or custom hook