前提
Redux React TypeScriptでsnsアプリを作っています。
apiを呼び出す際(fetchPostsAsync()の処理)にエラーが起きました。
TS2345: Argument of type 'AsyncThunkAction<PostsState[], undefined, {}>' is not assignable to parameter of type 'AnyAction'.
実現したいこと
エラーを無くしたい。
該当のソースコード
Post.tsx
1import React, { useEffect } from 'react' 2import { useDispatch } from 'react-redux'; 3import { useAppSelector } from '../../app/hooks'; 4import { selectPosts, fetchPostsAsync, selectStatus, Statuses } from './postSlice' 5 6function Posts() { 7 const posts = useAppSelector(selectPosts); 8 const status = useAppSelector(selectStatus); 9 10 const dispatch = useDispatch(); 11 12 useEffect(() => { 13 dispatch(fetchPostsAsync()); ←ここでエラー 14 }, [dispatch]) 15 16 let contents; 17 18 if (status !== Statuses.UpToDate) { 19 contents = <div>{status}</div> 20 } else { 21 contents = <div className='card'> 22 <div className='card-body'> 23 <h3>{status}</h3> 24 {/* form */} 25 {posts && posts.length > 0 && posts.map(post => { 26 return <div key={post.id} style={{ margin: "Sem" }}> 27 <h3>{post.title}</h3> 28 <p>{post.body}</p> 29 </div> 30 })} 31 </div> 32 </div> 33 } 34 35 return ( 36 <div> 37 <h1>投稿</h1> 38 <h3>{status !== Statuses.UpToDate}</h3> 39 contents {status} 40 <ul></ul> 41 </div> 42 ) 43} 44 45export default Posts
postSlice.tsx
1import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; 2import produce from "immer"; 3import { RootState } from "../../app/store"; 4import { fetchPosts } from "./postAPI"; 5 6export enum Statuses { 7 Initial = "not feched", 8 Loading = "Loading...", 9 UpToDate = "Up To Date", 10 Deleted = "Deleted", 11 Error = "Error" 12} 13 14export interface PostState { 15 id?: number, 16 title?: string; 17 body?: string, 18 created_at?: any, 19 updated_at?: any, 20} 21 22export interface PostsState { 23 posts: PostState[]; 24 status: string; 25} 26 27const initialState: PostsState = { 28 posts: [ 29 { 30 id: 1, 31 title: "", 32 body: "", 33 created_at: "", 34 updated_at: "", 35 } 36 ], 37 status: Statuses.Initial 38} 39 40export const fetchPostsAsync = createAsyncThunk<Array<PostsState>, undefined, {}>( 41 "posts/fetchPosts", 42 async (_, { rejectWithValue }) => { 43 try { 44 const response = await fetchPosts(); 45 return response; 46 } catch (error) { 47 return rejectWithValue('データ取得に失敗しました'); 48 } 49 } 50) 51 52export const postSlice = createSlice({ 53 name: "posts", 54 initialState, 55 /** 56 * Synchoronous actions 57 */ 58 reducers: {}, 59 extraReducers: (builder) => { 60 builder 61 .addCase(fetchPostsAsync.pending, (state) => { 62 return produce(state, (draftState) => { 63 draftState.status = Statuses.Loading; 64 }) 65 }) 66 .addCase(fetchPostsAsync.fulfilled, (state) => { 67 return produce(state, (draftState) => { 68 draftState.status = Statuses.UpToDate; 69 }) 70 }) 71 .addCase(fetchPostsAsync.rejected, (state) => { 72 return produce(state, (draftState) => { 73 draftState.status = Statuses.Error; 74 }) 75 }) 76 } 77}); 78 79export const { } = postSlice.actions; 80 81export const selectPosts = (state: RootState) => state.posts.posts; 82 83export const selectStatus = (state: RootState) => state.posts.status; 84 85export default postSlice.reducer; 86
postAPI.ts
1 2import { PostsState, } from "./postSlice"; 3 4const API_URL = "http://localhost:3000"; 5 6export async function fetchPosts() { 7 return fetch(`${API_URL}/posts.json`, { 8 method: "GET", 9 headers: { 10 "Content-Type": "application/json", 11 }, 12 }).then((response) => response.json()) 13 .catch((error) => { 14 console.log(error); 15 return {} as PostsState; 16 }) 17}
試したこと
こちらの記事を参考にcreateAsyncThunk<Array<PostsState>, undefined, {}>の部分をいじってみましたがダメでした。
https://zenn.dev/luvmini511/articles/c9cdb77a145f4d
補足情報(FW/ツールのバージョンなど)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。