reactjs - 如何在 React 中的两个按钮之间切换并根据单击的按钮更改 NAvlink 路径
问题描述
我正在尝试在反应中的两个图像之间切换并尝试根据路径更改 Navlink 路由在此处输入图像描述
<button className="lessonTypeButton">
<img
src={singleLessonsType}
alt="singleLessonsType"
className="packageOne"
onClick={lessonSelected}
/>
</button>
<br />
<br />
<br />
<button className="lessonTypeButton">
<img
src={packageLessons}
alt="packageLessons"
className="packageTwo"
onClick={lessonSelected}
/>
</button>
解决方案
您可以拥有href
在 URL 中具有路由的属性,
<button className="lessonTypeButton">
<img
src={packageLessons}
alt="packageLessons"
className="packageTwo"
onClick={lessonSelected}
href={/packageLessons}
/>
</button>
<button className="lessonTypeButton">
<img
src={singleLessonsType}
alt="singleLessonsType"
className="packageOne"
onClick={lessonSelected}
href={/singleLessonsType}
/>
</button>
或使用挂钩,
import { useHistory } from "react-router-dom";
然后在函数中使用它,
let history = useHistory();
function lessonSelected = ()=>{
history.push("/singleLessonsType");
}
或者直接调用 onclick 事件,
<button className="lessonTypeButton">
<img
src={packageLessons}
alt="packageLessons"
className="packageTwo"
onClick={history.push("/packageLessons");}
/>
</button>
<button className="lessonTypeButton">
<img
src={singleLessonsType}
alt="singleLessonsType"
className="packageOne"
onClick={history.push("/singleLessonsType");}
/>
</button>
推荐阅读
- javascript - 如何访问此网格的行对象?
- python - 如何检查 url 是否不在 i18n_pattern 中?
- mysql - Laravel whereBetween 不起作用。表明?
- android - 从后台带回片段时的Android片段生命周期
- ios - 在 Xcode 中禁用记录 UI 测试
- groovy - Groovy withBatch 无需关闭
- python - 如何在 matplotlib 中使用时间序列绘制单个行值
- java - 层次关系的图遍历
- machine-learning - 关于 XGBoost 的问题
- batch-file - 将文件从一台服务器复制到另一台服务器出现 NTFS 错误