Вот код, который сделает всю работу за вас
import React, { useState } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
const [carbrand, setCarBrand] = useState();
let cars = ["honda", "toyota", "bmw"];
let eachCarName = cars.map(name => {
console.log("name--->", name);
let carDiv = <div>{name}</div>;
return carDiv;
//return name;
});
console.log("eachCarName--->", eachCarName);
const eachCarClick = e => {
console.log("eachCarClick--->", e);
console.log("eachCarClick e.target.innerText--->", e.target.innerText);
};
// const setCarBrand = eachCarName;
// const carbrand = eachCarName;
return (
<div className="App">
<h1 onClick={eachCarClick}>{eachCarName}</h1>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Вот код и ящик, которые делают то, что вам нужно https://codesandbox.io/s/polished-cloud-fwj6h