Error: Expected ref to be a function, a string, an object returned by React.createRef(), or null.

信铁寒胜發表於2024-07-18

1、完整報錯:

Error: Expected ref to be a function, a string, an object returned by React.createRef(), or null.
    at coerceRef (react-dom.development.js:14873:1)
    at reconcileSingleElement (react-dom.development.js:15723:1)
    at reconcileChildFibers (react-dom.development.js:15808:1)
    at reconcileChildren (react-dom.development.js:19174:1)
    at updateHostComponent (react-dom.development.js:19924:1)
    at beginWork (react-dom.development.js:21618:1)
    at beginWork$1 (react-dom.development.js:27426:1)
    at performUnitOfWork (react-dom.development.js:26560:1)
    at workLoopSync (react-dom.development.js:26466:1)
    at renderRootSync (react-dom.development.js:26434:1)  

2、解決過程:

其實這個報錯的很簡單,就是你的物件,沒有ref 屬性。

很明顯,物件沒有ref 屬性。哪怕ref 為null。都可以

3、解決辦法:

把ref加上就好,直接賦值為null,也可以

相關文章