일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 자바스크립트
- 구문과 의미
- async-await
- 알고리즘
- 소오름~
- 자바스크립트의 탄생배경
- 프로그래머스
- Ajax란?
- 프로그래밍이란
- 가우스의 공식
- for반복문
- a && b
- ES6 브라우저 지원 현황
- JavaScript
- .map()
- array.reduce()
- 어려운데ㅠㅠ
- Math.min
- 자바스크립트의 특징
- !x.includes()
- 소름돋는 알고리즘
- 자바스크립트와 ECMAScript
- 행렬...
- 프로그래머스 공원 산책
- 삼항연산자
- .split()
- arr.push()
- 배열 최솟값
- for문
- Promise.all()
Archives
- Today
- Total
Ming's develop story
Chapter3 - 버킷리스트에 아이템 추가하기! 본문
App.js
원래 작성했던 버킷리스트 코드에 '추가하기'라는 버튼을 추가해주고,
addBucket 함수 추가 및 css 수정
import React from "react";
import BucketList from "./BucketList";
import styled from "styled-components";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
list: ["영화관 가기", "매일 책읽기", "수영 배우기"],
};
this.text = React.createRef();
}
componentDidMount() {}
addBucket = () => {
console.log(this.text.current.value);
const new_item = this.text.current.value;
// ... 스프레드 문법은 [1, ,2, 3] 또는 {a : b}
// 처럼 배열이나 딕셔너리 안의 요소를 밖으로 다 꺼내준다.
// [...this.state.list, 넣고 싶었던 어떤 값]
this.setState({ list: [...this.state.list, new_item] });
};
render() {
return (
<Appwrap className="App">
<Container>
<Title>내 버킷리스트</Title>
<Line />
<BucketList list={this.state.list} />
</Container>
<InputWrap>
<input type="text" ref={this.text} />
<button onClick={this.addBucket}>추가하기</button>
</InputWrap>
</Appwrap>
);
}
}
const Appwrap = styled.div`
.App {
background-color: #eee;
height: 100vh; /*vh는 화면을 기준으로 높이를 얼마나 줄지 정할 수 있다. */
width: 100vw;
display: flex; /*높이를 센터로 밀어넣어 주기위해 사용 */
flex-direction: column;
}
`;
const Container = styled.div`
background-color: #fff;
width: 50vw;
max-width: 350px;
margin: auto;
height: 80vh;
padding: 16px;
border: 1px solid #ddd;
border-radius: 5px;
`;
const Title = styled.h1`
color: slateblue;
text-align: center;
`;
const Line = styled.hr`
margin: 16px 0px;
`;
const InputWrap = styled.div`
background-color: #fff;
width: 50vw;
max-width: 350px;
margin: auto;
padding: 16px;
border: 1px solid #ddd;
border-radius: 5px;
`;
export default App;
|
여기서 스프레드 문법 ... 을 잘 활용해야 하는데
... 은 배열이나 딕셔너리 안의 요소를 밖으로 다 꺼내주는 역할을 한다.
여기서는 []로 감싸주면서 저 요소로 새로운 배열을 생성하는식으로 사용했다.
BucketList.js - 그대로
import React from "react";
import styled from "styled-components";
const BucketList = ({ list }) => {
const my_lists = list;
const my_wrap = React.useRef(null);
return (
<div ref={my_wrap}>
{my_lists.map((list, index) => {
return <ItemStyle key={index}>{list}</ItemStyle>;
})}
</div>
);
};
const ItemStyle = styled.div`
padding: 16px;
margin: 8px;
background-color: aliceblue;
`;
export default BucketList;
|
결과물
'스파르타코딩클럽 - 항해99 > 항해99 Chapter3 - react 주특기 기본' 카테고리의 다른 글
Chapter3 - Event Listener (0) | 2021.11.18 |
---|---|
Chapter3 - 2주차 숙제 (0) | 2021.11.18 |
Chapter3 - State 관리 (0) | 2021.11.17 |
Chapter3 - Ref를 사용해 리액트에서 돔요소 가져오기 (0) | 2021.11.17 |
Chapter3 - vscode-styled-components 확장패키지 (react) (0) | 2021.11.17 |
Comments