admin管理员组文章数量:1416050
function UpdateStatePlugin(props) {
...
const [editor] = useLexicalComposerContext();
...
}
function Notes() {
...
const initialConfig = {
...
};
return (
<LexicalComposer initialConfig={initialConfig}>
...
<UpdateStatePlugin />
</LexicalComposer>
)
}
This fails with 'useLexicalComposerContext' is not defined
I followed this guide and found 1 mention of someone running into a similar problem here. In both cases the structure seems to resemble what I have written. Would appreciate any help!
function UpdateStatePlugin(props) {
...
const [editor] = useLexicalComposerContext();
...
}
function Notes() {
...
const initialConfig = {
...
};
return (
<LexicalComposer initialConfig={initialConfig}>
...
<UpdateStatePlugin />
</LexicalComposer>
)
}
This fails with 'useLexicalComposerContext' is not defined
I followed this guide and found 1 mention of someone running into a similar problem here. In both cases the structure seems to resemble what I have written. Would appreciate any help!
Share Improve this question edited Feb 1, 2023 at 19:03 biinster asked Jan 16, 2023 at 8:10 biinsterbiinster 751 silver badge11 bronze badges2 Answers
Reset to default 3It's ok to define UpdateStatePlugin
in the same file. The important part is that UpdateStatePlugin
needs to be a child of LexicalComposer
.
Your error, 'useLexicalComposerContext' is not defined
, looks like one you'd see if you were missing an import. Try adding this to the top of your file:
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
You shouldn't be defining UpdateStatePlugin
within the render body of Notes
, you should define it outside as its own ponent.
function UpdateStatePlugin(props) {
const [editor] = useLexicalComposerContext();
// ...
}
function Notes() {
// return (...)
}
If you are doing this because you are creating UpdateStatePlugin
to use some outside variable, then you instead should be passing that in as a prop.
function Notes() {
const [someState] = useState();
function UpdateStatePlugin() {
useLexicalComposerContext();
// Let's say you are using `someState` here, this isn't "thinking in React"
doStuff(someState);
}
// ...
}
Instead you make the ponent accept someState
as a prop, and then pass it in during render
function UpdateStatePlugin(props) {
useLexicalComposerContext();
// Takes in `{ someState }` as a prop!
doStuff(props.someState);
}
function Notes() {
const [someState] = useState();
// ...
return <UpdateStatePlugin someState={someState} />;
}
本文标签: javascriptWhy can39t my custom Lexical React plugin use the LexicalComposerContextStack Overflow
版权声明:本文标题:javascript - Why can't my custom Lexical React plugin use the LexicalComposerContext? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745244147a2649478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论