English 中文(简体)
• 如何使图像出现并随着次年/次年/次月的纽芬兰语消失。
原标题:How to make an image appear and disappear with a button/Link in Next.js

I m New at React and Next, I have this Code:

import Link from  next/link ;
import Image from  next/image ;


async function getPalpedia() {
  const response = await fetch(`http://localhost:3000/api/palpedia`, {
    method: "GET",
  })
  return response.json();
}

export default async function palpedia() {
  const palpediaJSON = await getPalpedia();
  const palpedia = JSON.stringify(palpediaJSON)

  return (
    <div>
      <div className="fixed z-20 top-[4rem] bottom-0 right-[max(0px,calc(50%-58rem))] w-[15.5rem] py-10 overflow-y-auto hidden xl:block">
        {palpediaJSON.map((pal) => (
          <div className="palInfo" key={pal.id}>
            <Link href="#" >{pal.name}</Link>
            <p>Types: {pal.types.join(", ")}</p>
            <p>HP: {pal.hp}</p>
            <p>ATK: {pal.atk}</p>
            <p>DEF: {pal.def}</p>
          </div>
        ))}
      </div>
      <div className="overlay">
      <Image id="chikipi" src="/chikipi.png" width={0} height={0} alt="Chikipi" />
      </div>
    </div>
  );
}

This is a page in which I m trying to show a list called Palpedia, and when clicking a Link element, I would like the image to appear, of course I ll add a lot of images but first I m trying with this one. Also feel free to tell everything that is wrong/bad practice, as I made this without much knowledge.

谢谢!

I ve already tried by adding a "useState hook", but it seems this page belongs to the server side and I can only use that hook on the client side, so I do not know if I can apply that solution. I ve also tried to use the "use client" in the first line, same result.

问题回答

在这方面,你需要使用客户部分。

<代码>用户”;在代码上。 使用国家和使用效果。

这里是最后法典

"use client";

import { useEffect, useState } from "react";

import Link from  next/link ;
import Image from  next/image ;


async function getPalpedia() {
  const response = await fetch(`http://localhost:3000/api/palpedia`, {
    method: "GET",
  })
  return response.json();
}

export default function palpedia() {

  const [palpedia, setPalpedia] = useState(null);
  const [image, setImage] = useState(null);

  const handleClick = (imageUrl) => {
    if (imageUrl === image) {
      setImage(null);
    } else {
      setImage(imageUrl)
    }
  }
  
  useEffect(() => {
    async function fetchData() {
      const palpediaJSON = await getPalpedia();

      setPalpedia(palpediaJSON);
    }
    fetchData();
  }, []);

  return (
    <div>
      <div className="fixed z-20 top-[4rem] bottom-0 right-[max(0px,calc(50%-58rem))] w-[15.5rem] py-10 overflow-y-auto hidden xl:block">
        {palpedia.map((pal) => (
          <div className="palInfo" key={pal.id}>
            <Link href="#" onClick={() => handleClick(pal.image)}>{pal.name}</Link>
            <p>Types: {pal.types.join(", ")}</p>
            <p>HP: {pal.hp}</p>
            <p>ATK: {pal.atk}</p>
            <p>DEF: {pal.def}</p>
          </div>
        ))}
      </div>
      <div className="overlay">
      {image && (
        <Image id="chikipi" src={image} width={100} height={100} alt="Chikipi" />
      )}
      </div>
    </div>
  );
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签