react 7

[개발일기] 배열, 객체 복제하기

배열이나 객체를 복제할 수 있다. 먼저 배열로 예를 들어보자면 var arr = [1,2]; var arr2 = Array.from(arr); console.log(arr, arr2, arr === arr2); // 출력 결과물 = [1,2] [1,2] false Array.from()을 통해서 배열을 복제할 수 있고 arr와 arr2는 서로 다른 값이다. 객체 복제의 예로는 var a = {name:mandoo}; var b = Object.assign({},a); console.log(a,b,a===b); //출력결과 = {name:mandoo} {name:mandoo} false b.name = 'dooman'; console.log(a,b,a===b); //출력결과 = {name:mandoo} {..

[react] shouldComponentUpdate

성능을 향상시키기 위해서 컴포넌트의 render 함수가 실행될지 안될지를 개발자가 결정할 수 있도록 하는 특수한 약속의 함수 shouldComponentUpdate 가 있다. shouldComponentUpdate(newProps, newState){ console.log('=====toc render shouldComponentUpdate' ,newProps.data, this.props.data); return false; } 이 코드는 해당 컴포넌트의 render함수 전에 작성한다. newProps.data 로는 바뀐값을 알 수 있고, this.props.data 를 통해서 현재 값을 알 수 있다는 것이다. shouldComponentUpdate 함수의 결과값이 true 면 render함수가 호출되..

FrontEnd/react 2023.02.02

[개발일기] .concat

배열에 원소를 추가할 때 .push를 자주 쓰곤 했는데 .concat이라는 기능을 알게 되었다. .concat도 똑같이 배열에 원소를 추가하는데, .push 와 뭐가 다를까? 바로 .push는 배열의 원본에 원소를 추가하는것이라면, .concat은 배열의 원본은 건드리지 않고 조작하는 것이다. 코드로 예를 들자면, var arr = [1,2]; arr.push(3); // 이라면, arr를 출력했을 때 [1,2,3]이 출력된다. var arr2 = [1,2]; var result = arr2.concat(3); //이라면, result는 [1,2,3]이 되지만 arr2를 출력해보면 [1,2]가 출력된다. state의 값을 바꿔야 할 때 .push를 쓰지 말고, .concat처럼 새로운 데이터를 추가하는걸..

[react] State

컴포넌트 내부적으로 사용되는 것들이 state 이다. 리액트라는 시스템이 컴포넌트를 만들고 그 컴포넌트가 좋은 부품이 되기 위해서는 외부의 props에 따라서 컴포넌트를 실제로 구성하는 state가 철저히 분리되어있어야 한다. 사용과 구현의 분리가 확실하게 있는 상태로 양쪽의 편의성을 각자 도모하는 것이 좋은 부품을 만드는 것의 핵심이고, 리액트도 마찬가지이다. 아래 코드로 state를 공부해보자. class App extends Component { render(){ return ( ) }; } App 이라는 컴포넌트 안에 Subject라는 하위 컴포넌트가 있다. 하위 컴포넌트들의 props에 지금 하드코딩이 되어있는 것들을 state로 만들고 props로 전달해보자. 일단 먼저 constructor ..

FrontEnd/react 2022.12.28

[react] Props 기초

https://ko.reactjs.org/docs/components-and-props.html Components와 Props – React A JavaScript library for building user interfaces ko.reactjs.org props를 정의하는 공식 문서 링크 “props” (props는 속성을 나타내는 데이터입니다) 객체 인자를 받은 후 React 엘리먼트를 반환하므로 유효한 React 컴포넌트라고 정의한다. 저번에 만들었던 컴포넌트 코드 https://ojm1484.tistory.com/42 [react] 컴포넌트 만들기 src폴더 아래 App.js class Subject extends Component { render(){ return ( // 컴포넌트를 만들때..

FrontEnd/react 2022.12.27

[react] 컴포넌트 만들기

src폴더 아래 App.js class Subject extends Component { render(){ return ( // 컴포넌트를 만들때는 최상위 태그부터 만들어야한다. WEB world wide WEB ) }; } class App extends Component { render(){ return ( ) }; } 코드 작성 후 서버를 올려보면 이렇게 화면이 출력되는 것을 확인할 수 있다. 더 추가해 보자. class TOC extends Component { render(){ return ( HTML css js ) }; } class Content extends Component { render(){ return ( html html is hyperText Markup Language. ) ..

FrontEnd/react 2022.12.27