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

157
Views
Change img source in React using onMouseEnter while having a default picture

I am currently working on this where i have 4 buttons and each of them changes the image in a separate div. I have almost done it, though when the page reloads the image is blank and shows only when i hover over a button. I want the picture to be defaulted to the first option on page reload.

function App() {
  const [image, setImage] = useState('')

  return (
   <div>
      <button type="button" onMouseEnter={() => setImage(image1) }></button>
      <button type="button" onMouseEnter={() => setImage(image2) }></button>
      <button type="button" onMouseEnter={() => setImage(image3) }></button>
      <button type="button" onMouseEnter={() => setImage(image4) }></button>
   </div>
   <div>
      <img src={image} />
   </div>
    );
  }
export default App;
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Currently the default initial value is an empty string:

const [image, setImage] = useState('')

Change it to one of the values you want to use:

const [image, setImage] = useState(image1)
about 4 years ago · Santiago Gelvez 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!