Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
React - hide content with button

i'm new here. I got a problem with a task. I want a button that if you press it hides some content, a string in this case. If the content is hidden the button must change the text inside it, and it must say "show" and instead of hiding it shows the content previously hidden. If the content is already displayed, the button text will be "hide". I don't understand how to use if statement

...

import React { useState } from "react";

function App() {
  const [hideText, setHideText] = useState(false);

  const onClick = () => setHideText(false);

  return (
    <div>
      <button onClick={onClick}>Click me</button>
      {hideText ? <Text /> : null}
    </div>
  );
}

const Text = () => <div>I will disappear, true Magic</div>;
export default App;

...

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

import React { useState } from "react";

function App() {
  const [isVisible, setVisible] = useState(true);

  const onClick = () => setVisible(!isVisible);

  return (
    <div>
      <button onClick={onClick}>{isVisible? 'Hide' : 'Show'}</button>
      {isVisible? <Text /> : null}
    </div>
  );
}

const Text = () => <div>I will disappear, true Magic</div>;
export default App;
about 4 years ago · Santiago Trujillo Report

0

I don't know if I correctly understood your needs. I changed the variable name to be more meaningful :) Now the button shows Hide when the text is visible and Show when it's hidden. Clicking the button changes the state.

import React { useState } from "react";

function App() {
  const [isTextHidden, setTextHidden] = useState(true);

  const onClick = () => setTextHidden(!isTextHidden);

  return (
    <div>
      <button onClick={onClick}>{isTextHidden ? 'Show' : 'Hide'}</button>
      {!textHidden ? <Text /> : null}
    </div>
  );
}

const Text = () => <div>I will disappear, true Magic</div>;
export default App;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!