inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

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

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

해결된 질문

83

미니민

작성한 질문수 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. 질문 가이드라인 확인도 부탁드립니다!

useEffect와 lifecycle문의

0

31

2

프론트엔드 학습 수준 문의

0

43

2

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

0

51

2

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

0

56

2

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

0

80

2

이후의 커리큘럼 문의

0

102

2

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

0

50

2

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

0

85

3

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

1

82

3

Date 객체에 관련하여 질문드립니다.

0

85

2

리액트 개정판 교재 질문

0

60

2

예제코드가 안나와요!

0

78

2

select a variant 선택에서 javascript와 javascript+react compiler 중 무엇을 선택해야하나요? com

0

109

2

onMouseEnter 관련 문의 드립니다

0

93

3

배열의 렌더링 관련 질문 드립니다.

0

73

2

2:40초 refObj를 콘솔로 출력시 오류가 발생합니다.

0

113

2

TS, 리액트 강의중에 뭘 먼저 수강하는게 좋을까요?

0

137

2

useCallback 적용한 onCreate, onUpdate, onDelete 함수..

0

71

1

vs code 자동완성관련 문의

0

113

2

91강 useEffect내에서 상태변화함수 호출시 발생하는 에러

1

181

2

87강 필터 함수 질문

0

69

2

useRef, useState count 비교

0

67

2

안된다고했던 이유가 무엇이었는지 모르겠습니다

0

91

2

85강에서 객체를 왜 클래스로 만들어서 new 하지 않는건지 궁금합니다.

0

76

2