關(guān)于react中的常見(jiàn)錯(cuò)誤及解決
最近在做react項(xiàng)目的時(shí)候遇到了幾個(gè)報(bào)錯(cuò),這幾個(gè)報(bào)錯(cuò)在react項(xiàng)目還算常見(jiàn),因此記錄下來(lái)解決方法。
’type’ is missing in props validation
報(bào)錯(cuò):type缺少props驗(yàn)證
解決:
1.查看下propTypes是否寫(xiě)成大寫(xiě)了,因?yàn)槲覀円氲臅r(shí)候是大寫(xiě)的,所以很多小伙伴可能直接復(fù)制過(guò)來(lái)就成大寫(xiě)了,也會(huì)報(bào)錯(cuò)哦
2.新增type: PropTypes.number
import PropTypes from 'prop-types'; const ReportOperate = ({ logId, type }) => { return <> <a href='javascript:;' onClick={() => handleJump(record.logId, record.type)} style={{ color: '#1DA57A' }}>查看詳情</a> <a href={record.filePath} style={{ marginLeft: 20, color: '#1DA57A' }}>下載日志</a> </> } ReportOperate.propTypes = { logId: PropTypes.number, type: PropTypes.number,//加上這句就好了 } export default ReportOperate
throw new Error(‘Cyclic dependency’ + nodeRep)
Error:Cyclic dependency
報(bào)錯(cuò): Webpack 循環(huán)依賴(lài)
解決:
npm i --save-dev html-webpack-plugin@next
Cannot destructure property getFieldDecorator of ‘undefined’ or ‘null’.
報(bào)錯(cuò): 無(wú)法破壞getFieldDecorator屬性u(píng)ndefine或null
解決:
1.是否沒(méi)有注入Form.create()
// 無(wú)狀態(tài)組件 export default Form.create()(SearchForm) //有狀態(tài)組件 @Form.create()
Uncaught Error: Invariant Violation: Objects are not valid as a React child (found: object with keys {child}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Matchs.
報(bào)錯(cuò)原因: 對(duì)數(shù)組進(jìn)行map,然后取成了對(duì)象
解決:
1.map里面某一項(xiàng)取值是對(duì)象
Uncaught TypeError: Cannot read property ‘isSelectOptGroup’ of undefined
報(bào)錯(cuò)原因: const Option = Select.option 引入ant design 的select錯(cuò)誤
解決:
改成下面的
const { Option } = Select
VM38184:1 Uncaught (in promise) Error: Actions must be plain objects. Use custom middleware for async actions.
報(bào)錯(cuò)原因: 缺少了dispatch
解決:
改成下面的
backend.js:6 ./src/pages/beach/containers/Reward.js
Module not found: Error: Can’t resolve ‘antd/es/descriptions’ in '/Users/chenjiaxin/bull/src/pages/beach/containers’
或者
Module not found: Can’t resolve 'antd/es/affix’
報(bào)錯(cuò)原因:
在項(xiàng)目中使用了antd里面的Descriptions描述列表組件,發(fā)現(xiàn)報(bào)了這個(gè)錯(cuò)誤,根本原因就是使用的antd版本里面沒(méi)有這個(gè)組件,項(xiàng)目中引用的antd版本是3.16.0,而我看的文檔版本已經(jīng)到了3.24.0了
解決: 將antd 版本更新到最新或者文檔里的版本
Uncaught TypeError: Cannot convert undefined or null to object
報(bào)錯(cuò)原因: 由于undefined和null無(wú)法轉(zhuǎn)成對(duì)象,如果用Object.keys等操作的話(huà)需要加個(gè)對(duì)象作為初始值。
解決:
const [firstResponsibility, setFirstResponsibility] = useState({})
Uncaught TypeError: Cannot read property ‘value’ of null
報(bào)錯(cuò)原因: 涉及到React中的合成事件,debounce包裝后的回調(diào)函數(shù),是個(gè)異步事件,即e.target為null了
The SyntheticEvent is pooled. This means that the SyntheticEvent object will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.
解決:
//錯(cuò)誤代碼 <Search addonBefore={prefixSelector} style={{ width: 250 }} allowClear onChange={debounce(e => { console.log('e', e) e.persist() setSearchValue((e.target || {}).value) handleChangeParams(searchId, (e.target || {}).value) }, 1000)} /> // 錯(cuò)誤代碼,可以執(zhí)行,但是還是執(zhí)行多次,沒(méi)有起到防抖的效果 <Search addonBefore={prefixSelector} style={{ width: 250 }} allowClear onChange={e => { e.persist() debounce(() => { setSearchValue((e.target || {}).value) handleChangeParams(searchId, (e.target || {}).value) }, 2000)() }} /> // 正確代碼 <Search addonBefore={prefixSelector} style={{ width: 250 }} allowClear onChange={e => { e.persist() handleChangeValue(e) }} /> const handleChangeValue = debounce(e => { setSearchValue((e.target || {}).value) handleChangeParams(searchId, (e.target || {}).value) }, 1000)
vendor.js:1 Uncaught Error: Minified React error #306; visit https://reactjs.org/docs/error-decoder.html?invariant=306&args[]=()&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
報(bào)錯(cuò)原因: 標(biāo)簽內(nèi)的onClick事件立刻執(zhí)行導(dǎo)致死循環(huán),加載過(guò)多
解決:
// 錯(cuò)誤代碼 <CommonTabBox onCallBack={setCurTab} /> // 正確代碼 <CommonTabBox onCallBack={val => setCurTab(val)} />
無(wú)法使用 JSX,除非提供了 “–jsx” 標(biāo)志
解決:
在vscode的setting.json中設(shè)置
"typescript.tsdk": "node_modules\\typescript\\lib",
無(wú)法找到模塊“react/jsx-runtime”的聲明文件。
解決:
npm install -D @types/庫(kù)的名字
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于React動(dòng)態(tài)修改元素樣式的三種方式
這篇文章主要介紹了關(guān)于React動(dòng)態(tài)修改元素樣式的三種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08使用React和Redux Toolkit實(shí)現(xiàn)用戶(hù)登錄功能
在React中,用戶(hù)登錄功能是一個(gè)常見(jiàn)的需求,為了實(shí)現(xiàn)該功能,需要對(duì)用戶(hù)輸入的用戶(hù)名和密碼進(jìn)行驗(yàn)證,并將驗(yàn)證結(jié)果保存到應(yīng)用程序狀態(tài)中,在React中,可以使用Redux Toolkit來(lái)管理應(yīng)用程序狀態(tài),從而實(shí)現(xiàn)用戶(hù)登錄功能,需要詳細(xì)了解可以參考下文2023-05-05比ant更豐富Modal組件功能實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了比ant更豐富Modal組件功能實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11基于React的狀態(tài)管理實(shí)現(xiàn)一個(gè)簡(jiǎn)單的顏色轉(zhuǎn)換器
這篇文章主要介紹了用React的狀態(tài)管理,簡(jiǎn)簡(jiǎn)單單實(shí)現(xiàn)一個(gè)顏色轉(zhuǎn)換器,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08useEvent顯著降低Hooks負(fù)擔(dān)的原生Hook
這篇文章主要為大家介紹了useEvent顯著降低Hooks負(fù)擔(dān)的原生Hook示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07React模仿網(wǎng)易云音樂(lè)實(shí)現(xiàn)一個(gè)音樂(lè)項(xiàng)目詳解流程
這篇文章主要介紹了React模仿網(wǎng)易云音樂(lè)實(shí)現(xiàn)一個(gè)音樂(lè)項(xiàng)目的詳細(xì)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08一起來(lái)學(xué)習(xí)React元素的創(chuàng)建和渲染
這篇文章主要為大家詳細(xì)介紹了React元素的創(chuàng)建和渲染,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03