-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
54 lines (44 loc) · 1.39 KB
/
Copy pathapp.js
File metadata and controls
54 lines (44 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { animals } from './animals';//importing animals object from animals.js
import React from 'react';//importing React from react
import {createRoot} from 'react-dom/client';//import createRoot from react-dom/client
const container = document.getElementById('app');//getting a reference of the HTML element by it's id
const root = createRoot(container);//creating a React root to render content using createRoot() method and container as argument.
const title = 'Click an animal to know more about it!';
const background = <img
className= 'background'
alt='ocean'
src='/images/ocean.jpg'/>;
function displayFact(e) {
const animal = e.target.alt;
const index = Math.floor(Math.random()* animals[animal].facts.length);
const funFact = animals[animal].facts[index];
const p = document.getElementById('fact');
p.innerHTML = funFact;
}
const images = [];
for(const animal in animals) {
const image = (
<img
onClick={displayFact}
key={animal}
className='animal'
alt={animal}
src={animals[animal].image}
aria-label={animal}
role='button'
/>
);
images.push(image);
}
const showBackground = true;
const animalFacts = (
<div>
<h1>
{title || 'Click an animal for a fun fact!'}
</h1>
{showBackground && background}
<div className='animals'>{images}</div>
<p id='fact'></p>
</div>
);
root.render(animalFacts);