【发布时间】:2021-12-08 16:24:16
【问题描述】:
我在 React 中使用来自 Facebook 的 Draft JS。
当行以# Section name 开头时,我的draft 在文本上添加装饰器。在我页面上的另一个组件中,我想创建一个链接,当点击时它会向下滚动到该部分。
|-------| ------------|
|Nav | Draft editor|
|Title 1| # Title 1 |
|. | Some text |
我的装饰器非常基础:
{
strategy: (contentBlock: any, callback: any) => {
const REGEX =/^(# .+)$/
findWithRegex(REGEX, contentBlock, callback)
},
component: (props: any) => {
return <h3>{props.children}</h3>
}
}
我的导航链接
<a onClick={()=>{
window.scrollTo() // Scroll to Title 1 in DraftJS
}}>Title 1</a>
在过去的 JS 时代,我会想办法制作一个 ID 并获取元素,但这不是 react 方式。
感谢任何帮助。谢谢。
【问题讨论】:
-
react 也支持元素 by ref reactjs.org/docs/refs-and-the-dom.html