1

I got the following error when I was using redux:

TypeError: react__WEBPACK_IMPORTED_MODULE_2___default(...) is not a function

How do I solve this? I have added the code below.

dispatch() is being called in the component below

import authAction from "../store/Auth";
import { useDispatch } from "react-redux";
const useStyles = makeStyles((theme) => ({
  root: {
    flexGrow: 1,
  },
  menuButton: {
    marginRight: theme.spacing(2),
  },
  title: {
    flexGrow: 1,
  },
}));

export default function Header() {
  const classes = useStyles();
  const dispatch = useDispatch();
  console.log("Auth:", authAction);

  return (
    <div className={classes.root}>
      <AppBar position="static">
        <Toolbar>
          <Typography variant="h6" className={classes.title}>
            Counter
          </Typography>
          <Button
            color="inherit"
            onClick={() => {
              dispatch(authAction.login());
            }}
          >
            Login
          </Button>
        </Toolbar>
      </AppBar>
    </div>
  );
}

creatSlice component:

import { createSlice } from "@reduxjs/toolkit";

const authSlice = createSlice({
  name: "Auth",
  initialState: { loggedIn: false },
  reducers: {
    login(state) {
      state.loggedIn = true;
    },
  },
});

export const authActions = authSlice.actions;

export default authSlice.reducer;

configureStore component:

import { configureStore } from "@reduxjs/toolkit";
import auth from "./Auth";

const store = configureStore({
  reducer: {
    auth: auth,
  },
});

export default store;

Thank you.

Yoshiminea
  • 15
  • 6

1 Answers1

0

Issue

Your auth actions aren't the default export from your slice:

export const authActions = authSlice.actions;

export default authSlice.reducer;

but you are importing them as if they were:

import authAction from "../store/Auth";

Solution

Correctly import the named authActions export:

import { authActions } from "../store/Auth";

and dispatch the correct action creator:

<Button
  color="inherit"
  onClick={() => {
    dispatch(authActions.login());
  }}
>
  Login
</Button>
Drew Reese
  • 165,259
  • 14
  • 153
  • 181