inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

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

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

해결된 질문

89

미니민

작성한 질문수 2

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;

javascript react node.js

답변 1

0

이정환 Winterlood

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

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

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

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

Editor Component의 최적화 불가능 이슈

0

40

2

useCallback ⊂ useMemo

0

38

2

라이브러리 설치 질문

0

35

2

linter 질문

0

42

2

감정일기장 vercel 배포 후 새로 고침 시 404페이지가 뜨는 이유

0

43

2

Sort 메서드 관련 아티클

0

44

2

4.1) React.js를 소개합니다 강의를 듣고 궁금한 점이 생겼습니다.

0

46

2

html, body에 적용하는 css 스타일 질문입니다.

0

42

2

eslint.config.js 설정 질문입니다.

0

95

2

존재하지 않는 일기 url입력 시 alert이 두 번 떠요

0

70

1

교재(3쇄)와 강의 내용 문의

0

71

2

12.13) 하단 여백 스타일링 관련 질문 드립니다.

0

94

2

에러 질문드립니다

0

111

2

VSCode 설정 문의

0

145

2

PPT 코드 관련 질문

0

68

2

useEffect와 lifecycle문의

0

93

2

프론트엔드 학습 수준 문의

0

112

2

리액트 챕터별 코드에서 eslint 설정파일이 없어요

0

114

2

데이터 로딩중 화면만 계속 나와요!!

0

102

2

퍼블리셔일경우 어느정도 수준까지 강의를 들어야할까요

0

134

2

이후의 커리큘럼 문의

0

157

2

실슬환경 설정에서 save후 console.log 부분이 새로고침이 안되는현상입니다.

0

116

2

최적화 관련 질문있습니다 (useMemo 등)

0

134

3

프로바이더 컴포넌트의 위치는 어떤 기준인가요?

1

130

3