我使用https://www.npmjs.com/package/create-react-library创建了一个 React库,并在其他 React 项目中成功使用了它。但是当我尝试在库中使用 react hooks 功能时,它给了我以下错误。
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
.....
我的库 我只是尝试在我的组件中使用 useState 如下。
import React, { useState } from 'react'
const General = (props) => {
const [count, setCount] = useState(0);
return (
<div>
General component
</div>
)
}
export default General
我正在使用
"react": "^16.8.6"
和 "react-dom": "^16.8.6"
我的 React APP 使用https://github.com/facebook/create-react-app创建了一个应用程序,并使用了上面的库,如下所示。
import Reactfrom 'react'
import { General } from 'my.lib'
const accountSummary = props => {
return (
<div>
<General>
</div>
)
}
export default accountSummary
两者都具有相同的react版本,并且库使用了相同的react dom 和react版本 peerDependencies