错误:
TypeError: (0 , _react.useState) is not a function
我当前的反应版本:
"react": "^16.7",
"react-dom": "^16.7",
src / App.js:
import {memo, useState} from 'react'
export default memo(() => {
useState(false)
return 'OK'
})
src / App.test.js:
import React from 'react'
import App from './App'
import renderer from 'react-test-renderer'
it('renders without crashing', () => {
const tree = renderer.create(<App />).toJSON()
expect(tree).toMatchSnapshot()
})
如何解决此问题?
答案 0 :(得分:4)
根本原因是React V16.7没有钩子
解决方案是降级到
npm i react@next react-dom@next
自181220起为16.7.0-alpha.2:
yarn add react@next react-dom@next
然后丢失测试渲染器:
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<App />, div)
ReactDOM.unmountComponentAtNode(div)
})
这是package.json应该具有的内容:
"react": "^16.7.0-alpha.2",
"react-dom": "^16.7.0-alpha.2",