-
-
Notifications
You must be signed in to change notification settings - Fork 922
如何在react hook中使用Vditor #1080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
我用 id 还行,ref 可能并不是一个 element。JSX.Element 和 DOM 的 Element 应该还是有差别的。 |
你也使用的hook吗,我用id会有另一个报错:Uncaught (in promise) TypeError: Cannot read
properties of null (reading 'innerHTML')
at Vditor.init (index.min.js:15300)
at index.min.js:14971
能麻烦贴一下你的代码,或者在哪个项目中使用到了,我去翻一下看看
lixiang810 ***@***.***> 于2021年9月13日周一 下午10:48写道:
… 我用 id 还行,ref 可能并不是一个 element。JSX.Element 和 DOM 的 Element 应该还是有差别的。
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1080 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALIFJH3BJB7AJLTQ47O6A2TUBYFKXANCNFSM5DYYRQTQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
// App.tsx
import React from "react";
import Vditor from "vditor";
import "vditor/src/assets/scss/index.scss";
const App = () => {
const [vd, setVd] = React.useState<Vditor>();
React.useEffect(() => {
if (!vd) {
const vditor = new Vditor("vditor", {
after: () => {
setVd(vditor);
},
});
}
}, [vd]);
React.useEffect(() => {
if (vd) vd.setValue("`Vditor` 最小代码示例");
}, [vd]);
return <div id="vditor" className="vditor" />;
};
export default App; 依赖项上,需要安装 |
好的,谢谢,我再试试
lixiang810 ***@***.***> 于2021年9月14日周二 上午10:16写道:
… // App.tsx
import React from "react";
import Vditor from "vditor";
import "vditor/src/assets/scss/index.scss"
const App = () => {
const [vd, setVd] = React.useState<Vditor>();
React.useEffect(() => {
const vditor = new Vditor("vditor", {
after: () => {
setVd(vditor);
},
});
}, []);
return <div id="vditor" className="vditor" />;
};
export default App;
依赖项上,需要安装 sass 来正确显示样式,当然也可以在代码里加载 jsdelivr 上的 css 。
这是我的代码,其中 vd 会被设置为 Vditor 的实例以进行其他操作,如果不在其它地方使用的话,也可以不 useState()。
btw,react-vditor <https://github.com/HerbertHe/react-vditor> 的 Readme
里也有用 hook 的示例代码。
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1080 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALIFJH4QDYUHKH5VAZKIW63UB2WBTANCNFSM5DYYRQTQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
你的代码里,直接在组件函数内把 vditor 实例化是错误的。react 里,若组件状态有更新,则整个组件会被刷新,这样你那个实例化的过程就会被执行多次,带来性能上的损失,以及可能存在的 bug,比如你的代码实际上在自己的 DOM (return 的内容)被渲染出来之前就开始将 vditor 实例化了,这时 vditor 找不到那个 DOM。 |
另外我也建议作者更新一下 React 的示例代码。现在 React 流行 |
这是我测试的时候写的代码,因为他在第一步渲染的时候就出错了。我也试过在useEffect中使用,是相同的错误,因此我就专注于解决渲染的bug。现在看起来问题是在setValue上,之前我以为setValue是vditor的内置方法。一时乜有反应过来
lixiang810 ***@***.***> 于2021年9月14日周二 上午10:38写道:
… 另外我也建议作者更新一下 React 的示例代码。现在 React 流行 hook 增强的函数式组件,原来的类组件已经不那么常见了。我学
React 用的教程里,类组件被放在比较靠后的位置,成为了某种“高阶技巧 / 拓展阅读”。
React 官方 <https://reactjs.org/docs/hooks-faq.html> 是这么写的:We recommend
trying Hooks in new code.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1080 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALIFJH6TAS7HH6WATYCVRDTUB2YRXANCNFSM5DYYRQTQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
setValue 是 vditor 的内置方法。我代码里没有出现这个。我给改一下。 |
改好了,应该改得比较清楚了。 |
我也改好了。刚刚又试了一下,发现不在useEffect中是不能直接渲染的。我之前确实试过在不在useEffect中都是一样的,但现在的表现就是在useEffect中是可以的。😅难搞哦
lixiang810 ***@***.***> 于2021年9月14日周二 上午10:59写道:
… 改好了,应该改得比较清楚了。
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1080 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALIFJHZGPTVF4P4VXI5Z3L3UB23AHANCNFSM5DYYRQTQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
感谢你的回答,辛苦辛苦 |
遇到了个比较奇怪的问题. 初始化过程和你一样. 初始化的时候加了定制的toolbar. 如下
以上click被执行的时候会调用
但是这个位置打印出来的editor是空的.. editor声明如下: after后set值的
诡异的是下面这样的effect中. 是能够获取到editor的值, 但是上面的取消触发后就无法获取
|
@lixiang810 我发现不用去
|
我说的这个问题我找到问题点了. 主要是effect机制以及在其中初始化的问题. 需要监听editor的变更. 不然第一次after后执行获取的editor永远为空. |
@HengCC 和 Effect 确实有关,但是我觉得核心在于,setState 这个函数本质是异步的,你 setState 完了并不能直接用。 |
本质上是effect的机制决定的. 副作用如果不指定依赖的话,那么在当时的执行环境中如果editor没被初始化. 那就是获取不到值. 即使是依赖了也得这么写才能有值. useEffect(()=>{
if(!editor){
// 初始化 editor
}else{
//上面初始化后还会触发这个effect. 在这里可以做配置覆盖等. 或者调用方法. 本质上这个副作用只和editor相关.和上面的赋值markdown没啥关系. 这可能也是React官方推荐这样玩吧. 不同的依赖不同的副作用. 分离或者耦合组成不同的副作用.
}
},[editor]); |
描述问题
react hook中使用Vditor时,出现以下问题:
代码:
控制台报错:
期待的结果
能否出一个简单的react hook使用示例代码
版本信息
The text was updated successfully, but these errors were encountered: