src={clickedImageId !== "4" ? img2 : img4}
< /code>
Это мой код. Куда я ошибаюсь? < /P>
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { useState } from "react";
import img1 from "./img1.jpg";
import img2 from "./img2.jpg";
import img3 from "./img3.jpg";
import img4 from "./img4.jpg";
function App() {
const [clickedImageId, setClickedImageId] = useState("4");
const [galleryImg, setGalleryImg] = useState(img4);
function handleClick(e) {
setClickedImageId(e.target.id);
setGalleryImg(`img${e.target.id}`);
console.log(galleryImg, clickedImageId);
return;
}
return (
onClick={handleClick}
alt="rocks on the beach"
/>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
);
Подробнее здесь: https://stackoverflow.com/questions/797 ... ting-state
Мобильная версия