I am unable to use useSelector
and useDispatch
in export const function.
export const StartTelecalling = (piopiy, userId, password) => {
const { modal, isTestCall, isOnline } = useSelector((state) => state.calls);
const dispatch = useDispatch();
piopiy.login(userId, password, "sbcind.telecmi.com");
console.log("login Initiated..............");
piopiy.on("login", (object) => {
console.log(object);
// if (object.code === 200) {
console.log("login successfully......");
// dispatch(setIsOnline(true));
// }
});
}
In the above code I am getting invalid hook call at useSelector
and useDispatch
and I have multiple exports like that I can’t declare as default export.
2
Answers
You cannot call hooks inside nested functions, loops, conditions or regular JS functions as per React documentation. Also, React Hooks can only be called at the top level. Apparently, you are calling the hooks inside a regular JS function. Calling these hooks inside a functional react component might solve the issue.
Default export or not, you are breaking the Rules of Hooks:
StartTelecalling
is neither a React component nor a custom React hook.You can either convert
StartTelecalling
into a custom React hook, basically just rename it to use the"use-"
hook prefix naming convention, so that it can call other React hooks:Or refactor the function to have the selected state and
dispatch
function passed in as arguments.