[한 입 크기로 잘라 먹는 리액트] 리액트(React) 06

hidihyeonee·2025년 2월 7일
0
post-thumbnail

2025.02.07 작성

OS : Window
개발환경 : VScode
개발언어 : JavaScript
프레임워크 : React


뷰어 구현하기

사용자가 입력한 감정(emotion)과 일기 내용(content)을 시각적으로 보여주는 React 컴포넌트.

Viewer.js

import "./Viewer.css";
import { emotionList } from "../util";

const Viewer = ({ content, emotionId }) => {
    // console.log('emotionId', emotionId);
    const emotionItem = emotionList.find(it => it.id === emotionId);

    return (
        <div className="Viewer">
            <section>
                <h4>오늘의 감정</h4>
                <div
                    className={[
                        "emotion_img_wrapper",
                        `emotion_img_wrapper_${emotionId}`,
                    ].join(" ")}
                >
                    <img alt={emotionItem.name} src={emotionItem.img} />
                    <div className="emotion_descript">{emotionItem.name}</div>
                </div>
            </section>
            <section>
                <h4>오늘의 일기</h4>
                <div className="content_wrapper">
                    <p>{content}</p>
                </div>
            </section>
        </div>
    );
};

export default Viewer;

Viewer.css

.Viewer{

}

.Viewer section {
    width: 100%;
    margin-bottom: 100px;

    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}

.Viewer h4 {
    font-size: 22px;
    font-weight: bold;
}

.Viewer .emotion_img_wrapper {
    background-color: #ececec;
    width: 250px;
    height: 250px;
    border-radius: 5px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
}

.Viewer .emotion_img_wrapper_1 {
    background-color: #64c964;
}
.Viewer .emotion_img_wrapper_2 {
    background-color: #9dd772;
}
.Viewer .emotion_img_wrapper_3 {
    background-color: #fdce17;
}
.Viewer .emotion_img_wrapper_4 {
    background-color: #fd8446;
}
.Viewer .emotion_img_wrapper_5 {
    background-color: #fd565f;
}

.Viewer .emotion_descirpt {
    font-size: 25px;
    color: white;
}

.Viewer .content_wrapper {
    width: 100%;
    background-color: #ececec;
    border-radius: 5px;
    word-break: keep-all;
    overflow-wrap: break-word;
}

.Viewer .content_wrapper p {
    padding: 20px;
    text-align: left;
    font-size: 20px;
    font-family: "Yeon Sung";
    font-weight: 400;
    line-height: 2.5;
}

URL 파라미터로 일기 데이터 가져오기

React Router의 useParams와 커스텀 훅 useDiary를 활용하여 동적으로 데이터를 불러오는 방법.

import { useParams } from "react-router-dom";
import useDiary from "../hooks/useDiary";

const Edit = () => {
    const { id } = useParams();
    const data = useDiary(id);

    if (!data) {
        return <div>일기를 불러오고 있습니다..</div>
    } else {
        return <div>일기 수정 페이지</div>
    }
};

export default Edit;

삭제 기능

onDelete(id)를 호출하면 특정 ID의 일기가 삭제됨.

import { replace, useNavigate, useParams } from "react-router-dom";
import useDiary from "../hooks/useDiary";
import Button from "../component/Button";
import Header from "../component/Header";
import { useContext } from "react";
import { DiaryDispatchContext } from "../App";

const Edit = () => {
    const { id } = useParams();
    const data = useDiary(id);

    const navigate = useNavigate();
    
    const goBack = () => {
        navigate(-1);
    }

    const { onDelete } = useContext(DiaryDispatchContext);
    const onClickDelete = () => {
        if(window.confirm("일기를 정말 삭제할까요? 다시 복구되지 않아요.")) {
            onDelete(id);
            navigate("/", { replace: true});
        }
    }
    if (!data) {
        return <div>일기를 불러오고 있습니다...</div>
    } else {
        return (
            <div>
                <Header
                    title={"일기 수정하기"}
                    leftChild={<Button text = "< 뒤로가기" onClick={goBack} />}
                    rightChild={<Button type={"negative"} text={"삭제하기"} onClick={onClickDelete} /> } 
                    />
            </div>
        )}
};

export default Edit;

배포

nginx

nginx는 웹 서버 소프트웨어로, 가벼움과 높은 성능을 목표로 한다. 웹 서버, 리버스 프록시 및 메일 프록시 기능을 포함.

profile
벨로그 쫌 재밌네?

0개의 댓글