I have function like below
import React from "react";
import { useSelector, useDispatch } from "react-redux";
export const getCat = () => {
const lang = useSelector((state) => state.main.language);
return fetch("https://example.co/get/cat.php?lang="+lang, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
.then((response) => response.json())
.then((responseData) => {
return responseData;
})
.catch((error) => console.warn(error));
};
I want to access the state value using useselector and pass it to my url.but I am getting following error
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
But I have other files like below and it works fine.
const Lang = () => {
const lang = useSelector((state) => state.main.language);
console.log("lang--" + lang);
};
export default Lang;