我正在try 构建一个类似于Twitter如何在Next.js13中显示推文的组件,一旦点击,将指向该推文的Post-Page:/[username]/status/[tweet_id].我希望它只在你点击帖子本身时调用链接,而不是当你用任何按钮或推文中的内容点击时(除非点击的内容是文本).

My problem:

我可以让重定向正常工作,但由于某些原因,当您单击组件中的任何按钮或内容时,它仍会重定向.

What I'm trying to mimic:

以下是Twitter在点击推文内的按钮与点击帖子本身时的react :

enter image description here

点击点赞按钮不会重定向,但点击帖子本身会重定向.这就是我想要的.

My code:

import InteractionComponent from "./InteractionComponent";

import { BsDot, BsThreeDots } from "react-icons/bs";

import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import Link from "next/link";

import type { Tweet } from "@/lib/types/tweet.types";
import type { UserData } from "@/lib/types/userdata.types";

dayjs.extend(relativeTime);

export default async function PostComponent(props: { tweet: Tweet, userData: UserData }) {
  const { tweet, userData } = props;

  return (
    <div className="flex flex-col cursor-pointer hover:bg-slate/5">
      <Link href="/">
        <div className="flex flex-row border-b-2 p-4 border-slate/25">
          <div className="mr-3">
            <div className="h-[40px] w-[40px] bg-slate rounded-full"></div>
          </div>
          <div className="flex flex-col w-full">
            <div className="flex flex-row justify-between w-full">
              <div className="flex flex-row text-sm justify-between">
                <span className="font-bold">{tweet.authorInfo.authorDisplayName}</span>
                <div className="flex flex-row ml-2 font-light">
                  <span className="text-slate/75">@{tweet.authorInfo.authorUserName}</span>
                  <BsDot className="h-full text-slate/75" />
                  <span className="text-slate/75">{dayjs(tweet.createdAt).fromNow()}</span>
                </div>
              </div>
              <div>
                <BsThreeDots className="h-full text-slate/75" />
              </div>
            </div>
            <div id="post" className="text-sm font-light mb-2">
              <span>{tweet.textContent}</span>
            </div>
            <div className="flex flex-row text-slate/75 text-lg mt-2 justify-between w-full z-10">
              <InteractionComponent tweet={tweet} userData={userData} />
            </div>
          </div>
        </div>
      </Link>
    </div>
  )
}

What I already tried:

我试着移动<Link>组件,但似乎不起作用.我甚至试着在<InteractionComponent />的div上使用position: absolute,看看我是否能以某种方式越过链接(作为最后的努力),但这也没有起到作用……我甚至用谷歌搜索了这个问题,但我甚至不知道到底要用什么谷歌才能找到解决方案.我们非常感谢您的帮助,非常感谢!

推荐答案

这个问题的发生可能是因为100,我可以猜测,你的问题有两个解决方案.

  1. <Link>组件包装了整个tweet组件,包括按钮和内容,您可以修改这些按钮和内容,以仅包装作者信息和tweet内容.
  2. 单击按钮时停止传播.

类似于:

import InteractionComponent from "./InteractionComponent";
import { BsDot, BsThreeDots } from "react-icons/bs";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import Link from "next/link";
import { Tweet } from "@/lib/types/tweet.types";
import { UserData } from "@/lib/types/userdata.types";

dayjs.extend(relativeTime);

export default function PostComponent(props: { tweet: Tweet, userData: UserData }) {
  const { tweet, userData } = props;

  const handleClick = (e) => {
    e.stopPropagation();
  };

  return (
    <div className="flex flex-col cursor-pointer hover:bg-slate/5">
      <div className="flex flex-row border-b-2 p-4 border-slate/25">
        <div className="mr-3">
          <div className="h-[40px] w-[40px] bg-slate rounded-full"></div>
        </div>
        <div className="flex flex-col w-full">
          <div className="flex flex-row justify-between w-full">
            <Link href="/">
              <div className="flex flex-row text-sm justify-between cursor-pointer">
                <span className="font-bold">{tweet.authorInfo.authorDisplayName}</span>
                <div className="flex flex-row ml-2 font-light">
                  <span className="text-slate/75">@{tweet.authorInfo.authorUserName}</span>
                  <BsDot className="h-full text-slate/75" />
                  <span className="text-slate/75">{dayjs(tweet.createdAt).fromNow()}</span>
                </div>
              </div>
            </Link>
            <div>
              <BsThreeDots className="h-full text-slate/75" />
            </div>
          </div>
          <Link href="/">
            <div id="post" className="text-sm font-light mb-2 cursor-pointer">
              <span>{tweet.textContent}</span>
            </div>
          </Link> {/* NOTICE THIS */}
          <div className="flex flex-row text-slate/75 text-lg mt-2 justify-between w-full z-10">
            <InteractionComponent tweet={tweet} userData={userData} onClick={handleClick} /> {/* onClick applied HERE */}
          </div>
        </div>
      </div>
    </div>
  );
}

NOTE:您需要更改InteractionComponent的props 要求才能根据需要处理onClickprops .

Html相关问答推荐

后续使用收件箱从网页表中提取值

在CSS中不保持圆形图像形状的边框半径属性

Chrome和Safari浏览器中的CSS3动画不同

每次在视口中运行动画

如何使我的组织 struct 图的连接线响应?

如何创建剪切到路径的循环文本字幕?

轨道上的居中范围滑块拇指(Webkit)

Bootstrap 5.3.2模式页脚左填充还是左边距?

禁用所有行,但在16角中单击编辑按钮的行除外

页面文件夹内的 HTML 页面未加载主目录的 style.css

MUI 日期 Select 器要包含的日期

网页爬虫:使用时光机器进行数据采集

CSS 字母间距将按钮向右扩展

将图像高度调整到容器 div 中而不使其高度增加

pull-right 不适用于 bootstrap alert 内的按钮

什么没有 margin-right 和 width:100% 溢出子元素到左边?

Header 或 P 标记中的填充不能与 em 单元一起正常工作

如何向弹出窗口添加 sanitize: false 选项?

是否有一种静态方式可以根据暗模式 Select 一张或另一张图像?

如何将图像高度设置为等于文本高度?