Search code examples
typescriptredux-sagaredux-toolkit

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


Solution

  • 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 }};
       }