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

135
Views
Click Text and Change Text React

I have a question about onClick event using React hooks. I actually want the text to change to another text when user click and when user click again, it goes back to first text. For example user click text "Change First", then the text change to "Change Second". Then if user click the "Change Second", the text change to "Change First". It will change as long as user click the text.

This is my useState function:

const [shopStat, setshopChange] = useState("Change first");

And below is the part where I want to change the text:

<div className="featuredItem">
  <span className="featuredTitle">Your text is</span>
     <div className="featuredMoneyContainer">
        <span
         className="featuredMoney"
         style={{ color: green }}
         onClick={() => setshopChange("Change second")}
         >
         {shopStat}
         </span>
     </div>
</div>

However, the code above only change the text once to "Change Second" and I cannot click the text again to change it back to "Change First". Hope you all can help me. Thank you.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Set state functions accept curent state as parameter so you can use it like that.

onClick={() => setshopChange((currentState) =>  currentState === "Change second"  ? "Change First" : "Change second"  )}
about 4 years ago · Juan Pablo Isaza Report

0

import React, {useState} from 'react';
import './App.css';

function App() {
    const [shopStat, setshopChange] = useState(true);
    return (
    <main onClick={() => setshopChange(!shopStat)}>
        {shopStat === true ? "Change first" : "Change second"}
    </main>
 );
}

export default App;
about 4 years ago · Juan Pablo Isaza 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!