我已将我的项目更新为React 16.3.1,现在我收到此错误:
Uncaught Error: Element ref was specified as a string (map) but no owner was set. You may have multiple copies of React loaded. (details: https://fb(dot)me/react-refs-must-have-owner).
at invariant (invariant.js:42)
at coerceRef (react-dom.development.js:6736)
at createChild (react-dom.development.js:6977)
at reconcileChildrenArray (react-dom.development.js:7282)
at reconcileChildFibers (react-dom.development.js:7651)
at reconcileChildrenAtExpirationTime (react-dom.development.js:7756)
at reconcileChildren (react-dom.development.js:7747)
at updateHostComponent (react-dom.development.js:7998)
at beginWork (react-dom.development.js:8229)
at performUnitOfWork (react-dom.development.js:10224)
at workLoop (react-dom.development.js:10288)
at HTMLUnknownElement.callCallback (react-dom.development.js:542)
at Object.invokeGuardedCallbackDev (react-dom.development.js:581)
at invokeGuardedCallback (react-dom.development.js:438)
at renderRoot (react-dom.development.js:10366)
at performWorkOnRoot (react-dom.development.js:11014)
at performWork (react-dom.development.js:10967)
at requestWork (react-dom.development.js:10878)
at scheduleWorkImpl (react-dom.development.js:10732)
at scheduleWork (react-dom.development.js:10689)
at Object.enqueueSetState (react-dom.development.js:6212)
at Wrapper.../node_modules/react/cjs/react.development.js.Component.setState (react.development.js:242)
at Wrapper.onLoad (GoogleApiComponent.js:194)
at ScriptCache.js:39
我是一个新手,所以我不确定我应该找到什么来发现错误。
PS。我已经排除了多份副本选项,因为当我运行npm ls react
时,我得到了:
└── react@16.3.1
答案 0 :(得分:1)
根据对版本16.3更改的响应文档,不建议使用旧字符串ref api,现在您应按以下方式使用ref回调api
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.inputRef = React.createRef();
}
render() {
return <input type="text" ref={this.inputRef} />;
}
componentDidMount() {
this.inputRef.current.focus();
}
}