解決React報(bào)錯(cuò)Cannot assign to 'current' because it is a read-only property
總覽
當(dāng)我們用一個(gè)null
值初始化一個(gè)ref
,但在其類型中不包括null
時(shí),就會(huì)發(fā)生"Cannot assign to 'current' because it is a read-only property"錯(cuò)誤。為了解決該錯(cuò)誤,請(qǐng)?jiān)?code>ref的類型中包含null
。比如說(shuō),const ref = useRef<string | null>(null)
。
這里有個(gè)例子來(lái)展示錯(cuò)誤是如何發(fā)生的。
// App.tsx import {useEffect, useRef} from 'react'; const App = () => { const ref = useRef<string>(null); useEffect(() => { // ?? Error: Cannot assign to 'current' because it is a read-only property.ts(2540) ref.current = 'hello'; }, []); return ( <div> <h2>hello world</h2> </div> ); }; export default App;
上面例子的問(wèn)題在于,當(dāng)我們將null
作為初始值傳遞到useRef
鉤子中時(shí),并且我們傳遞給鉤子的泛型中沒(méi)有包括null
類型,我們創(chuàng)建了一個(gè)不可變的ref
對(duì)象。
正確的泛型
為了解決該錯(cuò)誤,我們必須在傳遞給鉤子的泛型中包括null
類型。
// App.tsx import {useEffect, useRef} from 'react'; const App = () => { // ??? include null in the ref's type const ref = useRef<string | null>(null); useEffect(() => { ref.current = 'hello'; }, []); return ( <div> <h2>hello world</h2> </div> ); }; export default App;
在ref
的類型中,我們使用聯(lián)合類型來(lái)包括null
類型,這使它成為可變ref
對(duì)象。
現(xiàn)在這個(gè)例子中ref
的類型是字符串或null
,它的current
屬性可以賦值為這兩種類型中的任意一種的值。
DOM元素
如果你的引用指向一個(gè)DOM元素,情況也是一樣的。如果你需要改變ref
的current
屬性的值,你必須將鉤子的類型定為 const ref = useRef<HTMLElement | null>(null)
。
注意,如果你不直接賦值給它的current
屬性,你不必在 ref
的類型中包含 null
。
// App.tsx import {useEffect, useRef} from 'react'; const App = () => { const ref = useRef<HTMLInputElement>(null); useEffect(() => { ref.current?.focus(); }, []); return ( <div> <input ref={ref} type="text" defaultValue="" /> </div> ); }; export default App;
上述例子中的ref
是用來(lái)聚焦input
元素的。因?yàn)闆](méi)有對(duì)其.current
屬性進(jìn)行賦值,所以沒(méi)有必要在其類型中包含null
。
原文鏈接:bobbyhadz.com/blog/react-…
以上就是解決React報(bào)錯(cuò)Cannot assign to 'current' because it is a read-only property的詳細(xì)內(nèi)容,更多關(guān)于React 報(bào)錯(cuò)assign current的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 解決React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'
- 解決React報(bào)錯(cuò)Property value does not exist on type HTMLElement
- 解決React報(bào)錯(cuò)Property?'value'?does?not?exist?on?type?EventTarget
- Can't?perform?a?React?state?update?on?an?unmounted?component報(bào)錯(cuò)解決
- Objects are not valid as a React child報(bào)錯(cuò)解決
- 解決React報(bào)錯(cuò)No duplicate props allowed
- 解決React報(bào)錯(cuò)`value` prop on `input` should not be null
- 解決React報(bào)錯(cuò)Property 'X' does not exist on type 'HTMLElement'
相關(guān)文章
react配置webpack-bundle-analyzer項(xiàng)目?jī)?yōu)化踩坑記錄
這篇文章主要介紹了react配置webpack-bundle-analyzer項(xiàng)目?jī)?yōu)化踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06React中用@符號(hào)編寫文件路徑實(shí)現(xiàn)方法介紹
在Vue中,我們導(dǎo)入文件時(shí),文件路徑中可以使用@符號(hào)指代src目錄,極大的簡(jiǎn)化了我們對(duì)路徑的書寫。但是react中,要想實(shí)現(xiàn)這種方式書寫文件路徑,需要寫配置文件來(lái)實(shí)現(xiàn)2022-09-09react-router browserHistory刷新頁(yè)面404問(wèn)題解決方法
本篇文章主要介紹了react-router browserHistory刷新頁(yè)面404問(wèn)題解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-12-12React18系列commit從0實(shí)現(xiàn)源碼解析
這篇文章主要為大家介紹了React18系列commit從0實(shí)現(xiàn)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01react?hooks頁(yè)面實(shí)時(shí)刷新方式(setInterval)
這篇文章主要介紹了react?hooks頁(yè)面實(shí)時(shí)刷新方式(setInterval),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03React中使用Echarts無(wú)法顯示title、tooltip等組件的解決方案
這篇文章主要介紹了React中使用Echarts無(wú)法顯示title、tooltip等組件的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03React?TypeScript?應(yīng)用中便捷使用Redux?Toolkit方法詳解
這篇文章主要為大家介紹了React?TypeScript?應(yīng)用中便捷使用Redux?Toolkit方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11react18中react-redux狀態(tài)管理的實(shí)現(xiàn)
本文主要介紹了react18中react-redux狀態(tài)管理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05