As can be seen in the image, I want to declare a react state hook to the alert of my webpage. At first, I want that there is no alert shown. And whenever user is clicking some button, I want to show a success alert with a message (error alert if something goes wrong). I will call showAlert function to do this with message and type as function parameter. That's why there can be more than one type of alert. So, I created this alert object and attached hook with initial value "NULL". But the editor gives me this error.
Argument of type '{ msg: any; type: any; }' is not assignable to parameter of type 'SetStateAction<null>'. Object literal may only specify known properties, and 'msg' does not exist in type '(prevState: null) => null'.ts(2345)
So, can someone tell me how I can tell useState the object parameters that I will allot it in future. Or should I try to hide the website alert in some other way? Here is the code..
const [alert, setAlert] = useState(null);
const showAlert = (message, type) => {
setAlert({
msg: message,
type: type,
});
setTimeout(() => {
setAlert(null);
}, 2000);
This solution is not working for me.
(initialState: S | (() => S)): [S, Dispatch>]; – Bittu Joshi Jul 05 '22 at 14:01(): [S | undefined, Dispatch>]; }' and '{ msg: any; type: any; }'.ts(2365)
The '|' operator is not allowed for boolean types. Consider using '||' instead.ts(2447)
3. Comparing to itself is potentially pointless.eslintno-self-compare
Object is possibly 'null'.ts(2531)