Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change headline text when clicking on multiple buttons in React

I’m not sure how to tackle this problem. I have two or more buttons and whenever I click on a button, I want my text to change. Any ideas?

import "./styles.css";

function App() {
  return (
    <div className="App">
      <h1>Change me!</h1>
      <button>Button 1</button>
      <button>Button 2</button>
      <button>Button 3</button>
    </div>
  );
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is a very basic react question. I advice you to read the react documentation:

import "./styles.css";

function App() {
  const [text, setText] = useState("Change me!");

  return (
    <div className="App">
      <h1>{text}</h1>
      <button onClick={() => setText("button 1")}>Button 1</button>
      <button onClick={() => setText("button 2")}>Button 2</button>
      <button onClick={() => setText("button 3")}>Button 3</button>
    </div>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading