import React, { useState, useEffect } from 'react'; import { useNavigate } from…
- User
import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { Box, Button } from '@yamada-ui/react'; import ResearchHeader from '../components/ResearchHeader'; import { useWindowSize } from "@uidotdev/usehooks";
const Research = () => { const navigate = useNavigate(); const size = useWindowSize(); const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null);
useEffect(() => { fetch('http://localhost:3101/api/v1/posts') .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then(data => setPosts(data)) // データをセット .catch(error => setError(error.message)) .finally(() => setLoading(false)); }, []);
if (loading) return Loading...; if (error) return Error: {error};
const handleHomeClick = () => { navigate('/'); };
const handleAddClick = () => { navigate('/Addtion'); };
const handleDetailClick = (id) => { navigate(
/post/${id}); // 詳細ページへのリンク };return ( <Box position="fixed" top={size.height * 0.2} left="50%" transform="translate(-50%, 0)" zIndex="2000" sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', // 中央揃え marginTop: '20px', // コンテナの上部に余白 }} > {posts.map((post) => ( <Button onClick={() => handleDetailClick(post.id)} sx={{ width: '300px', height: '50px', fontSize: '16px', color: 'black', backgroundColor: 'yellow', margin: '10px 0', display: 'flex', alignItems: 'center', justifyContent: 'center', }} > {post.cstmname} ))} <Box position="fixed" top={size.height * 0.75} left="50%" transform="translate(-50%, -50%)" zIndex="2000" sx={{ display: 'flex', flexDirection: 'row', // ボタンを水平に並べる alignItems: 'center', // 垂直方向の中央揃え justifyContent: 'center', // 水平方向の中央揃え gap: '10px', // ボタン間のスペース }} > ホーム 追加 ); };
export default Research; スクロールできるように
- Assistant
To make the list of posts scrollable within the
Researchcomponent, you can set a fixed height for the container that holds the list and apply theoverflow-y: autostyle to enable vertical scrolling. Here’s how you can achieve this:Modify the
Boxcomponent that contains the list to have a specified height and allow for scrolling:javascript