0

I have a misc.js wherein I have the following function which is meant to be used by several components:

export function loginCheck() {
  const dispatch = useDispatch();
  const user = useSelector((store) => store.user.currentUser);

  !user && dispatch(showLoginPrompt());
}

However it throws:

Invalid hook call. Hooks can only be called inside of the body of a function component.

According to the examples of custom hooks in react native, hooks can be used inside plain functions not functional components, so I don't understand what's wrong here. Any help is much appreciated!

Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
elijaveto
  • 31
  • 2

1 Answers1

0

Okay I solved it in a completely different way since I needed to be able to call the (use)loginCheck function within another function. Since this wasn't possible I now directly call a redux action like so:

const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    ...,
    checkLogin: (state) => {
      state.showLoginPrompt = !state.user;
    }
  },
})

now i can track the showLoginPromt state via useEffect

elijaveto
  • 31
  • 2