简体   繁体   中英

Expected 0 arguments, but got 1 while passing arguments to the async func in redux toolkit store

I am creating a project using Redux Toolkit + Typescript + React. I have the function the fetches movies in my moviesSlice (as part of redux toolkit store) that has movieId as an argument. When I try to dispatch this function in my component and pass this id, TypeScript shows an error: Expected 0 arguments, but got 1. How can I fix it? Thank you

 // moviesSlice.tsx export const fetchMovie = createAsyncThunk( "people/fetchPerson", async (movieId) => { try { const response = await axios(`${BASE_URL}/films/${movieId}`); return response.data; } catch (error) { console.log(error); } } ); // store.tsx export const store = configureStore({ reducer: { people: peopleReducer, movies: moviesReducer, pl.nets: pl.netsReducer, }, }); // Movie.tsx useEffect(() => { dispatch(fetchMovie(movieId)); }, []);

This is TypeScript. Give your arguments a type - otherwise the created thunk action creator will not know what to do:

export const fetchMovie = createAsyncThunk(
  "people/fetchPerson",
  async (movieId: string) => {
    try {
      const response = await axios(`${BASE_URL}/films/${movieId}`);
      return response.data;
    } catch (error) {
      console.log(error);
    }
  }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM