『React Hooksでデータを取得する方法』
2023/4/2 8:36:00
const [post, set_post] = useState<Post | undefined>(undefined);
useEffect(() => {
if (post==undefined && post_id) {
(async () => {
const post_db = (await API.graphql({
query: getPost,
variables: { id: post_id },
authMode: "API_KEY",
authToken: awsExports.aws_appsync_apiKey
}) as GraphQLResult<GetPostQuery>).data?.getPost;
if (post_db) {
set_post(post_db)
}
console.log({ post_db });
})();
}
}, [post_id]);
post==undefined
を外すと無限ループ)const [post, set_post] = useState<Post | undefined>(undefined);
if (post==undefined && post_id) {
(async () => {
const post_db = (await API.graphql({
query: getPost,
variables: { id: post_id },
authMode: "API_KEY",
authToken: awsExports.aws_appsync_apiKey
}) as GraphQLResult<GetPostQuery>).data?.getPost;
if (post_db) {
set_post(post_db)
}
console.log({ post_db });
})();
}