gpt4 book ai didi

javascript - 如何只显示一个面板?

转载 作者:行者123 更新时间:2023-12-02 22:03:50 25 4
gpt4 key购买 nike

如何确保在单击“显示”按钮的面板中仅显示 secret 文本。现在两个面板同时显示......

import React, { useState } from "react";
import "./styles.css";

export default function App() {
const [visible, setVisible] = useState(true);

const onHandleVisible = () => {
setVisible(!visible);
};

return (
<div className="App">
<div className="panel">
<h3>Panel1</h3>
<button onClick={onHandleVisible}>Show</button>
{!visible ? (
<div>
<h1>I am secret text</h1>
</div>
) : null}
</div>

<div className="panel">
<h3>Panel2</h3>
<button onClick={onHandleVisible}>Show</button>
{!visible ? (
<div>
<h1>I am secret text 2</h1>
</div>
) : null}
</div>
</div>
);
}

https://codesandbox.io/s/frosty-nash-nx4v6

最佳答案

建立 2 个状态

  const [visible1, setVisible1] = useState(true);
const [visible2, setVisible2] = useState(true);

return (
<div className="App">
<div className="panel">
<h3>Panel1</h3>
<button onClick={() => setVisible1(!visible1)}>Show</button>
{!visible1 ? (
<div>
<h1>I am secret text</h1>
</div>
) : null}
</div>

<div className="panel">
<h3>Panel2</h3>
<button onClick={() => setVisible2(!visible2)}>Show</button>
{!visible2 ? (
<div>
<h1>I am secret text 2</h1>
</div>
) : null}
</div>
</div>
);
}

关于javascript - 如何只显示一个面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com