TheDareback
TheDareback

Reputation: 407

Problem with expected 0 arguments, but got 1 in Redux Toolkit

I have an error Expected 0 arguments, but got 1. in simple Redux Slice file.

This error occurs when I try to dispatch.

App.tsx

useEffect(() => {
    dispatch(getMovies({ query: "searchQuery", page: 1 }));
  }, []);

Slice.tsx

const movieSlice = createSlice({
  name: "movie",
  initialState,
  reducers: {
    getMovies(name) {
      return name;
    },
    setMovies: (state, action: PayloadAction<Movie[]>) => {
      state.moviesList = action.payload;
    },
  },
});

export const {
  getMovies,
  setMovies,
} = movieSlice.actions;

Where is the problem? Where to state that getMovies can accept an object as an argument?

Thanks

Upvotes: 2

Views: 4648

Answers (1)

Chad S.
Chad S.

Reputation: 6633

All the reducer functions must take the current state as their first parameter, and optionally the action that triggered the reducer. Also your reducer functions need to be returning the new state, not just any arbitrary data.

So your getNames reducer is going to get state where you're trying to get name, you're missing the action (2nd parameter), and you're not returning the new state.

To pass the object as as you have there, your reducer should look something like:

   getMovies(state, {payload: {query, page}) {
      return {...state, moviesQuery: { query, page }};
   }

Upvotes: 2

Related Questions