reactjs 条件渲染,以获得显示隐藏按钮的输出,该按钮将显示、隐藏[关闭]下方的文本

8yoxcaq7  于 2023-02-08  发布在  React
关注(0)|答案(1)|浏览(219)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
enter image description here条件渲染不起作用。
已尝试条件呈现以获取显示隐藏按钮的输出,该按钮将显示隐藏下面的文本。

vs3odd8k

vs3odd8k1#

  • 您的代码结构不正确。为什么要返回函数组件之外的内容?
  • 如果你想在你点击按钮时隐藏/显示文本,你必须告诉按钮当你点击它时需要做什么(通过onclick)。
import './App.css';
import { useState } from 'react';

function App() {
  const [showText, setShowText] = useState(false);
  
  const handleClick = () => {
    setShowText(!showText);
  }
  
  return (
    <div className="App">
      <button onclick={handleClick}> Show/Hide </button>
      {showText == true && <h1> Hi There </h1> }
    </div>
  )
}

export default App;

相关问题