React "Use" Hook – Uncaught TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_0__.use) is not a function

huangapple go评论56阅读模式
英文:

React "Use" Hook - Uncaught TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_0__.use) is not a function

问题

I'm using react experimental with node 18 & trying to implement 'use' hook for handling a promise. Getting the following error and unable to find a solution. I assume it's something related to a Webpack configuration required.

package.json

"react": "experimental",
"react-dom": "experimental",

JS File

import { use, useState, Suspense } from 'react';

const Component = ({ url }) => {

    const fetchPromise = fetch(url).then(res => res.json());
    const data = use(fetchPromise);

    return (
        <div>
            {data}
        </div>
    );
}

Getting the following error

bundle.js:3509 Uncaught TypeError: (0, react__WEBPACK_IMPORTED_MODULE_0__.use) is not a function
    at Component (bundle.js:3509:58)
    at renderWithHooks (react-dom.development.js:16305:1)
    at mountIndeterminateComponent (react-dom.development.js:20074:1)
    at beginWork (react-dom.development.js:21587:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
    at invokeGuardedCallback (react-dom.development.js:4277:1)
    at beginWork$1 (react-dom.development.js:27451:1)
    at performUnitOfWork (react-dom.development.js:26557:1)
    at workLoopSync (react-dom.development.js:26466:1)
英文:

I'm using react experimental with node 18 & trying to implement 'use' hook for a handling a promise. Getting the following error an unable to find a solution. I assume it's something related to a Webpack configuration required.

package.json

&quot;react&quot;: &quot;experimental&quot;,
&quot;react-dom&quot;: &quot;experimental&quot;,

JS File

import {use, useState, Suspense} from &#39;react&#39;


const Component = ({url}) =&gt; {

    const fetchPromise = fetch(url).then(res =&gt; res.json());
    const data = use(fetchPromise);

    return (
        &lt;div&gt;
            {data}
        &lt;/div&gt;
    );
}

Getting the following error

bundle.js:3509 Uncaught TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_0__.use) is not a function
    at Component (bundle.js:3509:58)
    at renderWithHooks (react-dom.development.js:16305:1)
    at mountIndeterminateComponent (react-dom.development.js:20074:1)
    at beginWork (react-dom.development.js:21587:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
    at invokeGuardedCallback (react-dom.development.js:4277:1)
    at beginWork$1 (react-dom.development.js:27451:1)
    at performUnitOfWork (react-dom.development.js:26557:1)
    at workLoopSync (react-dom.development.js:26466:1)

答案1

得分: 1

以下是翻译好的部分:

"传递url作为属性确实是有道理的,应该这样做:"

const Component = ({ url }) => {
  const [data, setData] = useState()

  useEffect(() => {
    fetch(url)
      .then((res) => res.json())
      .then(_data => setData(_data))
  }, [])

  return <div>{data}</div>
}

"不要使用实验性版本的软件包,它们可能包含错误。"

编辑:

"我创建了一个示例,它可以正常工作:https://codesandbox.io/s/cool-glade-zh1w08?file=/src/App.js"

import { use } from "react";

const getProduct = fetch("https://dummyjson.com/products/1").then((res) =>
  res.json()
);

function App() {
  const product = use(getProduct);
  if (!product) return <div>Loading...</div>;

  return <div>{product.title}</div>;
}

export default App;

React "Use" Hook – Uncaught TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_0__.use) is not a function

英文:

It really make sense to pass url as a prop, this is how it should be done:

const Component = ({ url }) =&gt; {
  const [data, setData] = useState()

  useEffect(() =&gt; {
    fetch(url)
      .then((res) =&gt; res.json())
      .then(_data =&gt; setData(_data))
  }, [])

  return &lt;div&gt;{data}&lt;/div&gt;
}

Do not use experimental versions for your packages, they may contain bugs.

<br />

Edit:

I created an example and it works: https://codesandbox.io/s/cool-glade-zh1w08?file=/src/App.js

import { use } from &quot;react&quot;;

const getProduct = fetch(&quot;https://dummyjson.com/products/1&quot;).then((res) =&gt;
  res.json()
);

function App() {
  const product = use(getProduct);
  if (!product) return &lt;div&gt;Loading...&lt;/div&gt;;

  return &lt;div&gt;{product.title}&lt;/div&gt;;
}

export default App;

React "Use" Hook – Uncaught TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_0__.use) is not a function

huangapple
  • 本文由 发表于 2023年5月22日 03:01:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定