채널톡 아이콘

인프런 커뮤니티 질문&답변

미니민님의 프로필 이미지

작성한 질문수 2

한 입 크기로 잘라 먹는 리액트(React.js) : 기초부터 실전까지

12.13) New 페이지 구현하기 1. UI

월 변경 이벤트 핸들러가 작동하지 않습니다.

해결된 질문

작성

·

25

·

수정됨

0

코드는 아래와 같습니다. 다른 버튼들은 모두 클릭 이벤트 핸들러가 작동하는데, 월 변경 버튼에만 이벤트 핸들러가 작동하지 않습니다. 혹시 Date 객체를 잘 못 생성한 것일까요..?

 

import { useContext, useState } from "react";
import { DiaryStateContext } from "../App";

import Header from "../components/Header";
import Button from "../components/Header";
import DiaryList from "../components/DiaryList";


const getMonthlyDate = (pivotDate, data) => {
    const beginTime = new Date(pivotDate.getFullYear(), pivotDate.getMonth(), 1,0,0,0).getTime();
    const endTime = new Date(
        pivotDate.getFullYear(),
        pivotDate.getMonth()+1,
        0,
        23,
        59,
        59
    ).getTime();

    return data.filter((item)=> beginTime<= item.createdDate && item.createdDate <= endTime)
}

const Home = () => {
    const data = useContext(DiaryStateContext);

    //날짜 보관 
    const [pivotDate, setPivotDate] = useState(new Date());
    const monthlyData = getMonthlyDate(pivotDate, data);

    const onIncreaseMonth = () => {
        setPivotDate(new Date(pivotDate.getFullYear(), pivotDate.getMonth()+1));
    }


    const onDecreaseMonth = () => {
        setPivotDate(new Date(pivotDate.getFullYear(), pivotDate.getMonth()-1));
    }
    
    return <div>
        <Header title={`${pivotDate.getFullYear()} ${pivotDate.getMonth()+1}`}
            leftChild={<Button onClick={onDecreaseMonth} title={"<"}/>}
            rightChild={<Button onClick={onIncreaseMonth} title={">"}/>}
        />
        <DiaryList data={monthlyData}/>
    </div>
};

export default Home;

답변 1

0

이정환 Winterlood님의 프로필 이미지
이정환 Winterlood
지식공유자

안녕하세요 미니민님 이정환입니다.

현재 보여주신 코드 만으로는 정확한 문제의 원인을 식별하기 어렵습니다.

이에 전체 프로젝트 코드를 GiHub or GoogleDrive를 통해 링크로 남겨주시면 살펴보겠습니다.

PS. 질문 가이드라인 확인도 부탁드립니다!