프론트엔드
-
컴포넌트 리렌더링과 state 상태 (.feat 클린업)프론트엔드/프론트엔드 이슈 2024. 5. 29. 16:47
컴포넌트가 리렌더링된다고 해서 state 값이 초기화되는 것이 아니다. 아래와 같은 상황을 생각해보자!import React, { useState, useEffect } from 'react'import './App.css'function App(){ const [weather, setWeather] = useState(null) const [reset, setReset] = useState(true) const handleClick = () => setReset(false) useEffect(() => { console.log("날씨") if(reset){ setWeather("맑음") } // return ()..
-
API 요청 함수프론트엔드/프론트엔드 이슈 2024. 5. 20. 15:14
async function connectData(url, method, data = null, token = null){ const dataJson = await fetch(url,{ headers:{ 'Content-Type':'application/json', 'Authorization': `${token? 'Bearer' + token : ''}`, }, method: method, body: data? data : null }) const result = await dataJson.json() return result }API 데이터 요청하는 유틸리티 함수입니다. utils 폴더 생성하고, api.js 파일에 작성..
-
비주얼 스튜디오 코드 (vsc)에서 리액트 사용시 Emmet 적용하기프론트엔드/React 2024. 5. 17. 10:34
File -> Preferences -> Settings 로 이동한다.Workspace 탭에서 Extensions 메뉴를 열고, Emmet 을 선택한다.Preferences 에서 Edit in settings.json 링크를 클릭한다.{ "emmet.preferences": { }, "emmet.includeLanguages": { "javascript": "javascriptreact" }}settings.json 파일에 위와 같이 작성하고, 비주얼 스튜디오 편집기를 끄고 재시작한다.
-
함수와 this프론트엔드/Javascript 2024. 4. 20. 08:53
function 으로 생성한 함수에는 항상 this 와 prototype 속성이 존재한다. const Shadow = function(){ console.log(this) } console.log(Shadow()) this 는 윈도우 객체이다. console.log(new Shadow()) this 는 생성자함수이다. var Human = function(type){ this.type = type || 'human' } Human.isHuman = function(human){ return human instanceof Human } Human.prototype.breathe = function(){ alert('h-a-a-a-m') } 자바스크립트에서는 객체 복사로 상속한다. 복사하는 원본객체를 프로토..
-
비동기 - async, await프론트엔드/Javascript 2024. 3. 18. 16:33
* async 함수 async, await 문법을 사용하면 프로미스를 조금더 편하게 사용할 수 있다. async function f() { return 1 } function 앞에 async 를 붙이면 해당 함수는 항상 프로미스를 반환한다. 프로미스가 아닌 값(return 1)을 반환하더라도, 내부적으로는 해당 값(1)을 프로미스로 감싸서 반환한다. return 이 resolve 호출과 동일하다. async function f() { return 1 } f().then(alert) // 1 해당 코드를 실행하면 result 가 1인 이행 프로미스가 반환된다. 그러므로 then 을 연결해서 사용할 수 있다. async function f() { return Promise.resolve(1) } f().th..
-
비동기 - 프로미스 체이닝프론트엔드/Javascript 2024. 3. 15. 16:44
* 프로미스 체이닝 "비동기 - 콜백" 수업에서 스크립트를 순차적으로 불러오는 비동기 작업이 여러개인 경우에 콜백기반 프로그래밍을 사용하였다. 이때 콜백지옥(callback hell)과 같이 코드가 복잡해지는 문제가 발생하였다. 프로미스 체이닝을 이용하면 이러한 문제를 간단하게 해결할 수 있다. new Promise(function(resolve, reject) { setTimeout(() => resolve(1), 1000) }).then(function(result) { alert(result) // 1 return result * 2 }).then(function(result) { alert(result) // 2 return result * 2 }).then(function(result) { al..