reactjs - React、html 字符串和组件
问题描述
假设我们有一个 HTML 字符串。它看起来像这样:
"<p>Australia is the capital of Canada<footnote>No, it isn't</footnote></p>
<p>Australia's most famous animal<footnote>There are no animals in
Australia</footnote> is the panda</p>"
现在假设我们想要构建一个可以将该字符串转换为 HTML 的 react 组件,将所有脚注元素从<p>
标签中拉出,然后用一个显示有脚注的标记替换它们。像这样:
<p>Australia is the capital of Canada<a href="footnote-1">[1]</a></p>
<p>Australia's most famous animal<a href="footnote-2">[2]</a>is the panda</p>
...
<span id="footnote-1">No, it isn't</span>
<span id="footnote-2">There are no animals in Australia</span>
将这些脚注拉出并替换它们的“反应方式”是什么?有没有一种“反应方式”来进行这种操作?我目前的实现是这样的:
shouldComponentUpdate() {
return false;
}
// a whole lot of vanilla javascript
这并不理想。
我意识到这个问题的答案可能相当冗长,所以我很乐意只提供一些一般性提示。
解决方案
推荐阅读
- android - 在后台下载项目使用什么
- html - 彩色 DIV 元素末尾的形状
- ios - iOS Swift Parse-强制退出应用程序后调用解析云函数
- css - 在 Wordpress 投资组合列表中悬停时更改图像
- java - 如果我只调用 get、put、remove 并以线程 id 为键并且从不迭代映射,我是否需要 ConcurrentHashMap?
- ios - Visual Studio for Mac 是否支持使用分发配置文件调试 iOS 应用程序?
- react-native - create-react-native-app 打包程序在启动时卡住了
- sql - Active Record - 如果有任何子表匹配,则查询以从父表中排除条目
- android - 从最近窗格中删除应用程序后,无法从广播接收器启动活动
- dns - 领域驱动设计 - 如何聚合回合制游戏