0

Error while dispatching an action!

React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array react-hooks/exhausCompiled with warnings.

enter image description here App.js :-

function App() {
  const user = null;
  const dispatch = useDispatch();

  useEffect(() => {
    const unsubscribe = auth.onAuthStateChanged(userAuth => {
      if (userAuth) {
        dispatch(login({
          uid: userAuth.uid,
          email: userAuth.email
        }));
      } else {
        dispatch(logout);
      }
    })
    return unsubscribe;
  }, []);

userSlice.js :-

export const userSlice = createSlice({
  name: 'user',
  initialState: {
    user: null,
  },
  reducers: {
    login: (state, action) => {
      state.user = action.payload;
    }
  },
  logout: (state) => {
    state.user = null;
  }
});

export const { login, logout } = userSlice.actions;

store.js :-

import { configureStore } from '@reduxjs/toolkit';
import userReducer from '../features/userSlice';

export const store = configureStore({
  reducer: {
    user: userReducer,
  },
});
Ashmita Singh
  • 69
  • 2
  • 2
  • 11

0 Answers0