Hello!
I’m on this lesson (jsx-event-listeners): here.
The following code is used in the example, and I don’t understand how the makeDoggy
function, which takes a parameter e
is getting that parameter filled with an argument when it is called within the curly braces. Can someone explain how the function gets the necessary argument to operate correctly? (also why doesn’t it require instantiation with parentheses?)
import React from 'react';
import { createRoot } from 'react-dom/client';
const container = document.getElementById('app');
const root = createRoot(container);
function makeDoggy(e) {
// Call this extremely useful function on an <img>.
// The <img> will become a picture of a doggy.
e.target.setAttribute('src', 'https://content.codecademy.com/courses/React/react_photo-puppy.jpeg');
e.target.setAttribute('alt', 'doggy');
}
const kitty = (
<img
src="https://content.codecademy.com/courses/React/react_photo-kitty.jpg"
alt="kitty"
onClick={makeDoggy} />
);
root.render(kitty);