gpt4 book ai didi

reactjs - TypeError : Object(. ..) 在 React 版本 16.13.1 中不是 "useCallBack"的函数

转载 作者:行者123 更新时间:2023-12-04 02:34:10 25 4
gpt4 key购买 nike

我在浏览器的本地主机端口上收到一条错误消息,我还没有部署我的代码:

TypeError: Object(...) 不是一个函数

新事件src/events/pages/NewEvent.js:51


这里是我在 package.json 中的 react 版本:

  "dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "5.2.0",
"react-scripts": "^3.2.0",
"react-transition-group": "^4.4.1"
},

我在我的页面中导入了 NewEvent.js :

import React, {useCallBack , useReducer} from 'react'; 

在同一页面中,我在一个函数中嵌套了另一个函数:

  const [formState, dispatch] = useReducer(formReducer, {
inputs: {
title: {
value: '',
isValid: false
},
description: {
value: '',
isValid: false
}
},
isValid: false
});// this is the initial state that needs to be update in the reducer

const inputHandler = useCallBack((id,value,isValid) => {

dispatch({
type: 'INPUT_CHANGE',
value: value,
isValid: isValid,
inputId: id,
});
}, []);

问题在浏览器中显示为以下行:

  const inputHandler = useCallBack((id,value,isValid) => {

useCallBack is supposed to stop the code to loop when there is several function..I checked online in other post it seems to be link with the version of react, but I am not sure and quite confused.

这里是控制台中的消息:

log.js:24 [HMR] Waiting for update signal from WDS...
NewEvent.js:51 Uncaught TypeError: Object(...) is not a function

at NewEvent (NewEvent.js:51)
at renderWithHooks (react-dom.development.js:14803)
at mountIndeterminateComponent (react-dom.development.js:17482)
at beginWork (react-dom.development.js:18596)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at beginWork$1 (react-dom.development.js:23203)
at performUnitOfWork (react-dom.development.js:22154)
at workLoopSync (react-dom.development.js:22130)
at performSyncWorkOnRoot (react-dom.development.js:21756)
at scheduleUpdateOnFiber (react-dom.development.js:21188)
at updateContainer (react-dom.development.js:24373)
at react-dom.development.js:24758
at unbatchedUpdates (react-dom.development.js:21903)
at legacyRenderSubtreeIntoContainer (react-dom.development.js:24757)
at Object.render (react-dom.development.js:24840)
at Module../src/index.js (index.js:7)
at __webpack_require__ (bootstrap:784)
at fn (bootstrap:150)
at Object.1 (Users.js:19)
at __webpack_require__ (bootstrap:784)
at checkDeferredModules (bootstrap:45)
at Array.webpackJsonpCallback [as push] (bootstrap:32)
at main.chunk.js:1
index.js:1 The above error occurred in the <NewEvent> component:
in NewEvent (at App.js:23)
in Route (at App.js:22)
in Switch (at App.js:15)
in main (at App.js:14)
in Router (created by BrowserRouter)
in BrowserRouter (at App.js:11)
in App (at src/index.js:7)

Consider adding an error boundary to your tree to customize error handling behavior.
react-dom.development.js:22665 Uncaught TypeError: Object(...) is not a function
at NewEvent (NewEvent.js:51)
at renderWithHooks (react-dom.development.js:14803)
at mountIndeterminateComponent (react-dom.development.js:17482)
at beginWork (react-dom.development.js:18596)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at beginWork$1 (react-dom.development.js:23203)
at performUnitOfWork (react-dom.development.js:22154)
at workLoopSync (react-dom.development.js:22130)
at performSyncWorkOnRoot (react-dom.development.js:21756)
at scheduleUpdateOnFiber (react-dom.development.js:21188)
at updateContainer (react-dom.development.js:24373)
at react-dom.development.js:24758
at unbatchedUpdates (react-dom.development.js:21903)
at legacyRenderSubtreeIntoContainer (react-dom.development.js:24757)
at Object.render (react-dom.development.js:24840)
at Module../src/index.js (index.js:7)
at __webpack_require__ (bootstrap:784)
at fn (bootstrap:150)
at Object.1 (Users.js:19)
at __webpack_require__ (bootstrap:784)
at checkDeferredModules (bootstrap:45)
at Array.webpackJsonpCallback [as push] (bootstrap:32)
at main.chunk.js:1

浏览器上显示的信息:

NewEvent
src/events/pages/NewEvent.js:51
48 | isValid: false
49 | });// this is the initial state that needs to be update in the reducer
50 |
> 51 | const inputHandler = useCallBack((id,value,isValid) => {
| ^ 52 | //this function will received the id of the input that changed, value and answer whether it's valid or not
53 | dispatch({
54 | type: 'INPUT_CHANGE',

View compiled
16 stack frames were collapsed.
Module../src/index.js

有人知道这个问题吗?

-- 对不起,我的帖子很长 -- :D

最佳答案

所以我花了一些时间尝试重现此错误,最终意识到这是一个不幸的打字错误

简而言之,这样做:import React, { useCallback, useReducer } from "react";(注意回调中的小写 b ).

很难收集到实际错误是什么,仅给出这么多信息:TypeError: undefined is not a function

但是,MDN docs关于 TypeError 的(除其他事项外)状态:

Maybe there is a typo in the function name?

希望这会有所帮助!

关于reactjs - TypeError : Object(. ..) 在 React 版本 16.13.1 中不是 "useCallBack"的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62666064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com