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

111
Views
React / JSX Function not initialising onClick

I an trying to complete this JSX/react challenge. the issue I'm facing is the function 'displayFact' in running a single time when the program is loaded and not onClick as it should. How can I solve this bug?

import { animals } from './animals';
import React from 'react';
import ReactDOM from 'react-dom';

const title = ''

const background = (
  <img className='background' alt='ocean' src='/images/ocean.jpg' />
)

const images = [];
for (const animal in animals) {
  images.push(<img key={animal} className='animal' alt={animal} src={animals[animal].image} aria-label={animal} role='button' onClick={displayFact([animal])}/>)
};

function displayFact(e) {
  const targetAni = e
  const number = Math.floor(Math.random() * animals[e].facts.length);
  const funFact = animals[e].facts[number]
  document.getElementById('fact').innerHTML = funFact;
}

const animalFacts = (
  <div>
    <h1>{!title ? 'Click an animal for a fun fact' : ''}</h1>
    {background}
    <div className='animals'>
      {images}
    </div>
    <p></p>
  </div>
);



ReactDOM.render(animalFacts, document.getElementById('root'));
about 4 years ago · Juan Pablo Isaza
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!