Bad performance using useState
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 45/100
- Issue 类型
- 文档
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- javascript, react
调研方向
从 issue 中引用的第一个文档示例开始,检查其 onChange 回调如何使用 setValue。更新示例或随附的指导,解释重新渲染的成本以及何时适合使用 ref;当文档中的建议能够清楚地区分保存编辑器更改与有意更新 CodeMirror 值时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
I think the first example in the documentation has a flaw.
It uses setValue(val) in the onChange callback which means that React triggers re-rendering of the component. In my case, when using a computationally heavy SQL editor this gets slowed down so much that it even misses characters when typing.
const [value, setValue] = React.useState("console.log('hello world!');");
const onChange = React.useCallback((val, viewUpdate) => {
console.log('val:', val);
setValue(val);
}, []);
return <CodeMirror value={value} height="200px" extensions={[javascript({ jsx: true })]} onChange={onChange} />;
Here is the IMO correct way to do it:
const valueRef = React.useRef("console.log('hello world!');");
const [value, setValue] = React.useState(valueRef.current);
const onChange = React.useCallback((val, viewUpdate) => {
console.log('val:', val);
valueRef.current = val; // Update ref instead of state to avoid unnessecary re-renders
}, []);
return <CodeMirror value={value} height="200px" extensions={[javascript({ jsx: true })]} onChange={onChange} />;
So basically use setValue(val) only when you really want to change the code in your codemirror instance and thus want React to re-render your component. If you just want to store the internal state of your codemirror instance on changes for further use in your React component you should just use refs and access the actual value via valueRef.current.
- 主要语言
- TypeScript
- 星标
- 2.3k
- 派生
- 162
- 平均合并
- 2 小时 41 分钟
- 30 天内合并 PR
- 1
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
uiwjs/react-codemirror 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 64/100
uiwjs/react-codemirror#766 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
uiwjs/react-codemirror#765 ·
-
难度 3/5 1-2 天 新手友好度 35/100
uiwjs/react-codemirror#772 ·
-
难度 4/5 3-5 天 新手友好度 48/100
uiwjs/react-codemirror#770 ·
-
难度 3/5 1-2 天 新手友好度 48/100
uiwjs/react-codemirror#767 · 2 条评论 ·
查看 uiwjs/react-codemirror 的全部 Issue
相似的 Issue
-
bug P3 UI/UX
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
StellarState/SS-backend#712 ·
维护者通常 1 天内回复
-
area: backend bug
难度 2/5 1-3 小时 新手友好度 78/100
snapotter-hq/SnapOtter#3473 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
gitroomhq/postiz-app#2304 ·
维护者通常 1 天内回复
-
harlan-agent-ready-to-implement routine:ci-review
难度 2/5 1-3 小时 新手友好度 72/100
harlan-zw/unlighthouse#454 · 2 条评论 ·
维护者通常 1 天内回复