Trying to build a react otp component to publish it on npm. Getting this warning on using useState hook while testing the component in a test react app using npm link.
import React, { FC, useState } from "react";
interface Props {
numInputs: number;
}
const OtpInput: FC<Props> = ({ numInputs }): JSX.Element => {
const [otp, setOtp] = useState(new Array(numInputs || 4).fill(""));
return (
<div>
{otp.map((_, index) => {
return (
<React.Fragment key={index}>
<input type="number" />
</React.Fragment>
);
})}
</div>
);
};
export default OtpInput;
Warning while testing the component:
Warning: 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
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
at OtpInput (http://localhost:3000/main.f021bb100c5d3bb39209.hot-update.js:22:76)
at header
at div
at App
Tried removing react and react-dom from dev dependencies(both 18.2.0) and only having them as peer dependency as suggested in some other similar questions. But it didn't work. However if i remove the useState hook and just render some jsx component, it works fine. Can anyone please explain why am I getting this error?
Edit:
As suggested by @AdamThomas
Ran npm list react got this
├─┬ @testing-library/react@13.4.0
│ └── react@18.2.0 deduped
├─┬ react-dom@18.2.0
│ └── react@18.2.0 deduped
├─┬ react-otp-input-component@1.0.0 -> ./../react-otp-input-component
│ ├─┬ react-dom@18.2.0
│ │ └── react@18.2.0 deduped
│ └── react@18.2.0
├─┬ react-scripts@5.0.1
│ └── react@18.2.0 deduped
└── react@18.2.0
npm list react-dom returned
├─┬ @testing-library/react@13.4.0
│ └── react-dom@18.2.0 deduped
├── react-dom@18.2.0
└─┬ react-otp-input-component@1.0.0 -> ./../react-otp-input-component
└── react-dom@18.2.0