Dorothy_YANG
With Dorothy
Dorothy_YANG
전체 방문자
오늘
어제
  • 분류 전체보기 (279)
    • Hi, I'm Dorothy 🕵️‍♂️ (21)
      • Slowly but Surely (18)
      • IT certifications (3)
    • 🤯TIL (80)
      • HTML & CSS (2)
      • Javascript & jQuery (13)
      • React (13)
      • C언어 (1)
      • JAVA (22)
      • Python (2)
      • Oracle SQL (10)
      • My SQL (5)
      • Spring (12)
    • 💻Programmers (17)
    • 🏫 Open API_JAVA (101)
    • 🌎 Project (10)
      • Shopping (10)
    • 💥 Error (24)
    • ⚙ Setting (23)

블로그 메뉴

  • 홈
  • 방명록

공지사항

인기 글

태그

  • Eclipse
  • Javascript
  • 서버등록
  • 코딩앙마
  • 파이썬온라인
  • 콜라보레이토리
  • 이것이자바다
  • HTML
  • 기간쿼리
  • 연습문제
  • 창초기화
  • SQL
  • 오류해결
  • 시작일종료일
  • Database
  • 비쥬얼스튜디오코드
  • SQLD합격
  • 노마드코더
  • SQLD합격후기
  • oracle
  • AllArgsConstructor
  • CSS
  • spring
  • sql기간
  • java
  • 독학후기
  • colaboratory
  • googlecolaboratory
  • 기간설정
  • 백준

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Dorothy_YANG

With Dorothy

🤯TIL/React

[한입 크기로 잘라 먹는 리액트] React에서 배열 사용하기 1 - 리스트 렌더링 (조회)

2023. 11. 19. 16:24
728x90

1. DiaryList.js 파일 생성

2. App.js에 추가

3. 임시배열 만들어서 prop으로 전달

4. DiaryList가 undefined로 전달될 경우 에러 해결

5. DiaryItem.js 생성


 

App.js

import "./App.css";
import DiaryEditor from "./DiaryEditor";
import DiaryList from "./DiaryList";

const dummyList = [
  {
    id: 1,
    author: "도로시",
    content: "하이 1",
    emotion: 5,
    created_date: new Date().getTime()
  },
  {
    id: 2,
    author: "나로시",
    content: "하이 2",
    emotion: 3,
    created_date: new Date().getTime()
  },
  {
    id: 3,
    author: "다로시",
    content: "하이 3",
    emotion: 1,
    created_date: new Date().getTime()
  },
  {
    id: 4,
    author: "라로시",
    content: "하이 4",
    emotion: 5,
    created_date: new Date().getTime()
  },
  {
    id: 5,
    author: "마로시",
    content: "하이 5",
    emotion: 5,
    created_date: new Date().getTime()
  },
];

function App() {
  return (
    <div className="App">
      <DiaryEditor />
      <DiaryList diaryList={dummyList} />
    </div>
  );
}

export default App;

 


DiaryList.js

import DiaryItem from "./DiaryItem";

const DiaryList = ({ diaryList }) => {
  console.log(diaryList);
  return (
    <div className="DiaryList">
      <p>일기 리스트</p>
      <h5>{diaryList.length}개의 일기가 있습니다.</h5>
      <div>
        {diaryList.map((it) => (
          <DiaryItem key={it.id} {...it} />
        ))}
      </div>
    </div>
  );
};

DiaryList.defaultProps = {
  diaryList: [],
};

export default DiaryList;

DiaryItem.js

const DiaryItem = ({ author, content, created_date, emotion, id }) => {
  return (
    <div className="DiaryItem">
      <div className="info">
        <span>
          작성자 : {author} | 감정점수 : {emotion}
        </span>
        <br />
        <span className="date">{new Date(created_date).toLocaleString()}</span>
      </div>
      <div className="content">{content}</div>
    </div>
  );
};

export default DiaryItem;

 

App.css

/* App */
.App {
}

/* editor */

.DiaryEditor {
  border: 1px solid gray;
  text-align: center;
  padding: 20px;
}

.DiaryEditor input,
textarea {
  margin-bottom: 20px;
  width: 500px;
}

.DiaryEditor input {
  padding: 10px;
}
.DiaryEditor textarea {
  padding: 10px;
  height: 150px;
}

.DiaryEditor select {
  width: 300px;
  padding: 10px;
  margin-bottom: 20px;
}

.DiaryEditor button {
  width: 500px;
  padding: 10px;
  cursor: pointer;
}

/*  List */

.DiaryList {
  border: 1px solid gray;
  padding: 20px;
  margin-top: 20px;
}

.DiaryList h2 {
  text-align: center;
}

/* List Item */

.DiaryItem {
  background-color: rgb(240, 240, 240);
  margin-bottom: 10px;
  padding: 20px;
}

.DiaryItem span {
  margin-right: 10px;
}

.DiaryItem .info {
  border-bottom: 1px solid gray;
  padding-bottom: 10px;
  margin-bottom: 10px;
}
.DiaryItem .date {
  color: gray;
}

.DiaryItem .content {
  margin-bottom: 30px;
  margin-top: 30px;
  font-weight: bold;
}

.DiaryItem textarea {
  padding: 10px;
}

 

 


  • 출처
    https://inf.run/N9fZn
 

한입 크기로 잘라 먹는 리액트(React.js) : 기초부터 실전까지 - 인프런 | 강의

개념부터 독특한 프로젝트까지 함께 다뤄보며 자바스크립트와 리액트를 이 강의로 한 번에 끝내요. 학습은 짧게, 응용은 길게 17시간 분량의 All-in-one 강의!, 리액트, 한 강의로 끝장낼 수 있어요.

www.inflearn.com

 

728x90
저작자표시 비영리 변경금지 (새창열림)

'🤯TIL > React' 카테고리의 다른 글

[한입 크기로 잘라 먹는 리액트] React에서 배열 사용하기 3 - 데이터 삭제하기  (0) 2023.11.19
[한입 크기로 잘라 먹는 리액트] React에서 배열 사용하기 2 - 데이터 추가하기  (0) 2023.11.19
[한입 크기로 잘라 먹는 리액트] React에서 DOM 조작하기 - useRef  (0) 2023.11.17
[한입 크기로 잘라 먹는 리액트] React에서 사용자 입력 처리하기  (0) 2023.11.17
[React vs JavaScript] React란?  (0) 2023.11.04
    '🤯TIL/React' 카테고리의 다른 글
    • [한입 크기로 잘라 먹는 리액트] React에서 배열 사용하기 3 - 데이터 삭제하기
    • [한입 크기로 잘라 먹는 리액트] React에서 배열 사용하기 2 - 데이터 추가하기
    • [한입 크기로 잘라 먹는 리액트] React에서 DOM 조작하기 - useRef
    • [한입 크기로 잘라 먹는 리액트] React에서 사용자 입력 처리하기
    Dorothy_YANG
    Dorothy_YANG
    Slowly but Surely, 비전공 문과생의 개발공부

    티스토리툴바