reactjs - 如何点击鼠标打开下一页
问题描述
TLDR
我想
nested routing
在 React 中实现。我可以使用什么库?
问题
今天是个好日子!我有标题。对于每个标题,我都有一个描述。例如,我想改变文字,当我点击标题2时,请注意下面的例子。我想知道什么最适合用于此类目的?反应路由器或其他什么?例如一些 npm 包。还有如果网上有这样的例子或教训,请给我一个链接
例子
解决方案
这是使用简单useState
钩子的示例。
完整代码:
import React, { useState } from "react";
import "./styles.css";
const data = [
{
id: 1,
title: "Heading1",
description:
"Heading 1 Good day, everyone! \nI have headers. For each title, I have a description. \nFor example, I want the text to change, when I click on heading 2, please pay attention to the example, https://ibb.co/8mCHK5y \nI want to know what is best to use for such purposes? react-router or something else? for example some npm package. And what if there are such examples or lessons on the Internet, please leave me a link"
},
{
id: 2,
title: "Heading2",
description:
"Heading 2 Good day, everyone! I have headers. For each title, I have a description. For example, I want the text to change, when I click on heading 2, please pay attention to the example, https://ibb.co/8mCHK5y I want to know what is best to use for such purposes? react-router or something else? for example some npm package. And what if there are such examples or lessons on the Internet, please leave me a link"
},
{
id: 3,
title: "Heading3",
description:
"Heading 3 Good day, everyone! \nI have headers. For each title, I have a description. \nFor example, I want the text to change, when I click on heading 2, please pay attention to the example, https://ibb.co/8mCHK5y \nI want to know what is best to use for such purposes? react-router or something else? for example some npm package. And what if there are such examples or lessons on the Internet, please leave me a link"
},
{
id: 4,
title: "Heading4",
description:
"Heading 4 Good day, everyone! \nI have headers. For each title, I have a description. \nFor example, I want the text to change, when I click on heading 2, please pay attention to the example, https://ibb.co/8mCHK5y \nI want to know what is best to use for such purposes? react-router or something else? for example some npm package. And what if there are such examples or lessons on the Internet, please leave me a link"
},
{
id: 5,
title: "Heading5",
description:
"Heading 5 Good day, everyone! \nI have headers. For each title, I have a description. \nFor example, I want the text to change, when I click on heading 2, please pay attention to the example, https://ibb.co/8mCHK5y \nI want to know what is best to use for such purposes? react-router or something else? for example some npm package. And what if there are such examples or lessons on the Internet, please leave me a link"
},
{
id: 6,
title: "Heading6",
description:
"Heading 6 Good day, everyone! \nI have headers. For each title, I have a description. \nFor example, I want the text to change, when I click on heading 2, please pay attention to the example, https://ibb.co/8mCHK5y \nI want to know what is best to use for such purposes? react-router or something else? for example some npm package. And what if there are such examples or lessons on the Internet, please leave me a link"
}
];
export default function App() {
const [index, setIndex] = useState(0);
return (
<div className="container">
<div className="headings">
{data.map((d, i) => (
<button
onClick={() => {
setIndex(i);
}}
>
{d.title}
</button>
))}
</div>
<div className="description">
<div className="content">{data[index]["description"]}</div>
</div>
</div>
);
}
PS:我正忙着编写这个东西,并没有看到你的最新评论,天哪。但无论如何都要发布它,我希望它对某人有所帮助。
推荐阅读
- python - Pymongo 基本示例失败“ssl 模块不可用”
- python - 在 Jupyter Notebook 中,使用 geopandas,x-y 轴没有显示
- javascript - 如何从角度的弹簧启动中获得错误响应
- sql - 如何使用聚合函数加入视图?
- swiftui - SwiftUI LazyVStack 孩子在滚动时消失
- python - Python Selenium 我只从循环中获取第一个元素
- c++ - 错误:std::string 未在此范围内声明,在 for 循环 C++ 中
- kubernetes - 如何在 Kubernetes Operator 中执行无缝升级
- android - 如何将 bytearray 转换为 pdf 并以 ACTION_VIEW 意图显示
- c# - 在 ASP.NET Core MVC 中显示每个人的总价值