웹의 동작방식:
그렇다면 서버리스(serverless)란?
DOM이란?
자바스크립트 문법 (ES6 문법)
class MyCat extends Cat {
constructor (name, age):
super(name); // 여기서 super는 부모 클래스의 constructor를 호출한다.
this.age = age;
}
showName() {
return '내 고양이 이름은' + super.showName() + '입니다';
}
let array = [1,2,3,4,5]
let new_array = [...array]
let info = {name: "mean0", id : 0};
let is_me = info.name === "mean0"?true: false;
Array의 4가지 내장 함수들
const text_array = Array.from('hello', (item, idx) => {return idx});
// 값이 정해지지 않았을 때
const undecided_array = Array.from({length:4}, (item, idx) => {return idx});
JSX 문법
JSX 문법 규칙
Component
컴포넌트에서 데이터를 다루는 방법 (State and Props)
State: 컴포넌트가 가지고 있는 데이터 (내가 마음대로 삭제, 수정할 수 있는 데이터)
Props: 보통 부모 컴포넌트로 부터 가져오는 데이터
컴포넌트를 예쁘게 꾸미는 방법 (SASS, SCSS)
SCSS 기본 문법:
div {
background-color: green
&:hover { background-color: blue}
//scss
$bgColor : yellow;
.App{
background-color: #{$bgColor}
}
Styled-components: 리액트 컴포넌트 스타일링 방법중 하나. (SCSS, 변수선언해서 style을 먹여주는 등 다양한 방법중에 하나다).
Styled-components 장점
import styled from 'styled-components';
const App(props) => {
return (
<div className = "App">
<MyStlyed bgColor = {"red"}>Hello React</Mystyled>
</div>
);
}
const MyStyled = styled.div`
width: 50vw,
min-height : 150px,
padding: 10px,
border-radius: 15px,
color: #fff,
&hover : {
background-color: ${(props) => (props.bgColor)};
}
`
어떨 때 DOM을 새로 그릴까?
라이프 사이클?
알아두면 좋은 라이프 사이클 함수!
라이프 사이클 함수는 클래스형 컴포넌트에서만 (constructor, render()) 사용할 수 있다.
constructor(): 생성되었을 때 호출되는 함수
render(): 컴포넌트의 모양을 브라우저에 보여주는 함수
componentDidMount(): 컴포넌트가 화면에 나타났을 때 호출되는 함수. 첫 렌더링할때만 사용되는 함수이다.
componentDidUpdate(): 컴포넌트의 데이터를 변환했을 때 호출되는 함수 (리렌더링), 이전의 state&props 데이터와 현재 state&props 데이터를 비교할 때 쓸 수 있다.
componentDidUpdate(prevProps,prevState){
console.log(prevProps,prevState)
}
Ref! 리액트에서 돔 요소를 가져오려면? React.ref함수를 사용해보자!
import React from "react";
class App extends React.Component{
constructor(props){
super(props);
this.state = { list: ['영화관 가기', '매일 책읽기', '수영배우기'],};
this.text = React.createRef(); //버튼의 인풋값을 가져오고 싶을 때 쓴다.
}
componentDidMount(){
console.log(this.text);
console.log(this.text.current); //input값을 current라는 변수명 안에다가 담아두고 있다.
}
render(){
return(
<div>
<input type ="text" ref = {this.text}/>
</div>
}
class App extends React.Component{
constructor(props){
super(props);
this.state = {
count : 3,
};
}
render(){
return(
<div className = "App">
<Nemo>nemo</Nemo>
</div>
)
}
}
const Nemo = styled.div`
width : 150px;
height: 150px;
background-color : #eee;
margin: 10px;
`;
styled-components를 만들어서 컴포넌트를 꾸며주는것과 style = {{}}바로 먹여주는 거에서 문법의 차이가 조금씩 있다는 것을 인지해야한다. 예를들어, styled-components에서는 background-color with #eee라고 명시하지만 태그에 style을 직접 쓸때에는 backgroundColor라고 하고 그리고 '#eee'이렇게 한다.
Class형 컴포넌트에서 state관리 방법: setState
class App extends React.Component {
constructor(props){
super(props);
this.state = {
count: 3,
}
}
addNemo = () => {
this.setState({this.state.count +1};)
}
removeNemo = () => {
this.setState({this.state.count -1);)
}
render(){
return(
)
함수형 컴포넌트에서 state관리 방법: useState
import React from "react;
const Namo = (props) => {
const [count, setCount] = React.useState(); // 첫번째 인자(count)는 state값을 불러다 주는 변수를 가져온다. setState는 나중에 데이터 변경시킬 때 함수 명을 말한다.
const nemo_count = Array.from({length: count}, (v,i) => {return i});
const addNemo = () => {
setCount(count +1);
}
const removeNemo = () =>{
if(count > 0){setCount(count-1)}
else{window.alert("더 이상 네모가 없습니다");}
}
}
return (<div className = "App">
{nemo_count.map((num, idx) => {
return(
<Nemo key = {idx}>nemo<Nemo>
)})
}
Event Listener란?