英文:
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
"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)
答案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;
英文:
It really make sense to pass url
as a prop, this is how it should be done:
const Component = ({ url }) => {
const [data, setData] = useState()
useEffect(() => {
fetch(url)
.then((res) => res.json())
.then(_data => setData(_data))
}, [])
return <div>{data}</div>
}
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 "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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论