Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

69
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;
6 months 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)
6 months ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.