可以将createroot与打字稿一起使用 - typeError:m.createroot不是函数

发布于 2025-02-13 13:32:28 字数 2548 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2025-02-20 13:32:29

我在将应用程序从React 17升级到React 18时遇到了相同的问题。该应用最初是使用Create-react-app my-app -template tyspercript

错误:

TypeError: m.createRoot is not a function

修复我的修复程序是更新index.html以使用React 18脚本而不是更早的React 17。基本上,这是有道理的,因为React无法在React 17脚本中找到Croteroot的定义有能力的更新以使用React 18脚本时读取定义。

之前:当使用react 17

 <script crossorigin src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>            
 <script crossorigin src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>

升级/ 解决问题:使用React 18时,

<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>

该问题应该包含在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:

TypeError: m.createRoot is not a function

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 of createRoot in react 17 scripts and able to read the definition when updated to use react 18 scripts.

Before: when using react 17

 <script crossorigin src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>            
 <script crossorigin src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>

after the upgrade/ to resolve the issue: when using react 18

<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文