尝试在ReactDOM.render中使用React Hooks.对吗



所以我得到了dispatcher.useEffect is not a function

react.development.js:1463 Uncaught TypeError: dispatcher.useEffect is not a function
at useEffect (react.development.js:1463)
at Preload (VM75697 Preload.js:40)
at ProxyFacade (react-hot-loader.development.js:677)
at mountIndeterminateComponent (react-dom.development.js:14563)
at beginWork (react-dom.development.js:15063)
at performUnitOfWork (react-dom.development.js:17820)
at workLoop (react-dom.development.js:17860)
at HTMLUnknownElement.callCallback (react-dom.development.js:149)
at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
at invokeGuardedCallback (react-dom.development.js:256)

我正在尝试使用渲染方法中的函数:

render(
<Root store={store} history={history} />,
document.getElementById('app'),
)

我的Root.js文件看起来像:

import React from 'react'
import { hot } from 'react-hot-loader'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
import Preload from '../Preload'
import Routes from './Routes'    
const Root = ({ store, history }) => (
<Provider store={store}>
<Preload>
<ConnectedRouter history={history}>
<Routes />
</ConnectedRouter>
</Preload>
</Provider>
)
export default hot(module)(Root)

最后我在预加载.js 中使用useEffect

import { useEffect } from 'react'
import { connect } from 'react-redux'
import { init } from '../actions'
const Preload = ({
children,
token,
onInit,
}) => {
useEffect(() => {
if (token) {
onInit()
}
})
return children
}
export default connect(
state => ({
token: state.security.token,
}),
dispatch => ({
onInit: () => dispatch(init()),
}),
)(Preload)

这个错误的大多数答案是,人们忘记在useEffect中使用函数,但在我的情况下不是这样。

答案很简单。更新到测试版不仅是react,而且也是react-dom

看起来react-reduxconnect给我

Uncaught Error: Hooks can only be called inside the body of a function component.

因为CCD_ 6只在6版本(现在是beta版本(中开始支持hook。

相关内容

  • 没有找到相关文章

最新更新