可以将createroot与打字稿一起使用 - typeError:m.createroot不是函数
我正在使用React 18和传单从事一个项目。 (我不认为传单是问题的一部分,但可能值得一提。)我试图在地图上添加自定义元素,但是使用Croteroot可以给我type> typeserror:m.croteroot不是函数< /代码>。 我想使用的内容的代码示例在这里:
import { MutableRefObject, ReactElement, useEffect } from 'react'
import L, { Map } from 'leaflet'
import { createRoot } from 'react-dom/client'
interface MapControlElementProps {
mapRef: MutableRefObject<Map | null>
button: ReactElement
position: L.ControlPosition
}
export default function TestElement(props: MapControlElementProps) {
useEffect(() => {
const { mapRef, button, position } = props
if (mapRef.current) {
const MapHelp = L.Control.extend({
onAdd: () => {
const controlElement = L.DomUtil.create(
'div',
'leaflet-control leaflet-bar'
)
const root = createRoot(controlElement)
root.render(button)
return controlElement
},
})
const control = new MapHelp({ position })
control.addTo(mapRef.current)
return () => {
control.remove()
}
}
return () => {}
}, [props])
return null
}
唯一使用的是使用
ReactDOM.render(button, controlElement)
测试:
Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot
尽管ReactDom.Render有效,但我更喜欢使用Croteroot,因为我在其他地方使用了18种。
更新了2023年3月2日的情况
,我已经将问题跟踪到react-dom软件包,react-dom/client.js。
'use strict';
var m = require('react-dom');
if (process.env.NODE_ENV === 'production') {
exports.createRoot = m.createRoot;
exports.hydrateRoot = m.hydrateRoot;
} else {
var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
exports.createRoot = function(c, o) {
i.usingClientEntryPoint = true;
try {
return m.createRoot(c, o);
} finally {
i.usingClientEntryPoint = false;
}
};
exports.hydrateRoot = function(c, h, o) {
i.usingClientEntryPoint = true;
try {
return m.hydrateRoot(c, h, o);
} finally {
i.usingClientEntryPoint = false;
}
};
}
到目前为止 如果确实要出版,则选择名称,但我知道什么。但是,周围有些戳,这表明我确实没有一个称为createroot的功能。现在该怎么办,我仍然不知道。
I'm working on a project using React 18 and Leaflet. (I don't believe Leaflet is part of the problem but it might be worth mentioning.) I'm trying to add custom elements to the map, but using createRoot gives me TypeError: m.createRoot is not a function
.
The code sample of what i'm trying to use is here:
import { MutableRefObject, ReactElement, useEffect } from 'react'
import L, { Map } from 'leaflet'
import { createRoot } from 'react-dom/client'
interface MapControlElementProps {
mapRef: MutableRefObject<Map | null>
button: ReactElement
position: L.ControlPosition
}
export default function TestElement(props: MapControlElementProps) {
useEffect(() => {
const { mapRef, button, position } = props
if (mapRef.current) {
const MapHelp = L.Control.extend({
onAdd: () => {
const controlElement = L.DomUtil.create(
'div',
'leaflet-control leaflet-bar'
)
const root = createRoot(controlElement)
root.render(button)
return controlElement
},
})
const control = new MapHelp({ position })
control.addTo(mapRef.current)
return () => {
control.remove()
}
}
return () => {}
}, [props])
return null
}
The only thing that works is using
ReactDOM.render(button, controlElement)
But tests are complaining about that:
Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot
Although ReactDOM.render works, I would much prefer to use createRoot because i'm using 18 everywhere else.
Update to the situation 2nd of March 2023
So far i have tracked the issue to react-dom package, react-dom/client.js where there is this code:
'use strict';
var m = require('react-dom');
if (process.env.NODE_ENV === 'production') {
exports.createRoot = m.createRoot;
exports.hydrateRoot = m.hydrateRoot;
} else {
var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
exports.createRoot = function(c, o) {
i.usingClientEntryPoint = true;
try {
return m.createRoot(c, o);
} finally {
i.usingClientEntryPoint = false;
}
};
exports.hydrateRoot = function(c, h, o) {
i.usingClientEntryPoint = true;
try {
return m.hydrateRoot(c, h, o);
} finally {
i.usingClientEntryPoint = false;
}
};
}
First of all, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED seems like an odd choice for name if this was indeed meant to be published but what do i know. However, a bit of poking around shows me that var m indeed doesn't have a function called createRoot. What to do with this now, i still don't know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在将应用程序从React 17升级到React 18时遇到了相同的问题。该应用最初是使用Create-react-app my-app -template tyspercript
错误:
修复我的修复程序是更新
index.html
以使用React 18脚本而不是更早的React 17。基本上,这是有道理的,因为React无法在React 17脚本中找到Croteroot
的定义有能力的更新以使用React 18脚本时读取定义。之前:当使用react 17
升级/ 解决问题:使用React 18时,
该问题应该包含在React升级文档中,但是在此响应时,我找不到它,在其他地方找不到任何帮助。希望这将帮助某人:)
参考:
I have encountered the same issue while upgrading our app from React 17 to React 18.The app was originally created
using create-react-app my-app --template typescript
Error:
The fix for me is to update the
index.html
to use react 18 scripts instead of earlier react 17. Basically, this makes sense as react couldn't find the definition ofcreateRoot
in react 17 scripts and able to read the definition when updated to use react 18 scripts.Before: when using react 17
after the upgrade/ to resolve the issue: when using react 18
This should have been included in the react upgrade documentation but at the time of this response I couldn't find it and couldn't find any help elsewhere. Hopefully this will help someone :)
References: