본문 바로가기

자바스크립트7

[JS] Array.prototype.sort()의 허점 06.20.20241. JavaScript Sort Method의 이상한 점JS를 쓰면서 sort를 사용해야 할 일이 있을 때 사실 빌트인 정렬 함수를 많이 쓰곤 했다.굳이 정렬 메소드를 재생성해야할 필요가 없었기 때문이다. 그런데 유데미 강의를 들으며 이상한 점을 알게되었다.["B", "C", "A"].sort();// ["A", B", "C"][6, 4, 15, 10].sort();// [10, 15, 4, 6] 자바스크립트에서는 이 코드가 동작하지 않는다는 것이다.분명 sort 메소드를 쓸 때 실행 오류를 한 번도 마주한 적이 없는데 이런 기본적인 함수에서 숫자가 정렬되지 않는다는게 믿겨지지가 않았다.2. Why?왜 자바스크립트의 정렬 함수는 숫자를 정렬하지 못하는 것일까?그 답은 공식 문서에서 찾.. 2024. 6. 20.
메모이제이션이란?(useMemo, useCallback) in React.js 06.26.2023 1. 메모이제이션의 정의 같은 함수가 같은 값을 계속 리턴해야할 때, 첫 번째 값을 메모리에 저장해놓고 메모리에서 값을 불러와 재사용하는 메모리 사용 기법. 2. 활용도 2-1. useMemo 예를 들어 이러한 함수형 컴포넌트가 있다고 하자. function Component () { const result = calculate(); return {result} } function calculate () { // 무거운 작업을 하는 코드 return 10; } 이 컴포넌트가 렌더링이 될 때마다 calculate은 무거운 작업을 계속해야 할 것이다. 그러나 result는 항상 10일 때, 성능이 비효율적이 될 것. 이럴 때 메모이제이션을 사용하여 해결 가능 function Compone.. 2023. 6. 26.
5. Query Callback (쿼리콜백) in Node.js 0. Problem app.post('/api/rate', (req,res) => { const {residentName, quality, diff, willing, comment, tags} = req.body; let id = 0; db.query(`SELECT id FROM resident WHERE name = ?;`, [residentName],(err,result)=>{ if(err) throw err; console.log(result); id = result[0].id; }); const query = `INSERT INTO rate(resident_id, quality_rate, difficulty_rate, willingness, comment${tagQueryFront} VALUES .. 2023. 4. 11.
4. Using Variables in useEffect (useEffect에서 변수 쓰기) 03.16.2023 0. Background What I wanted to do was to use the prop as name for components and fetching data from database when props was passed in the result page from the main page. Here is what I did. 1. Store the prop as name using useState function Result(props){ const [name, setName] = useState(null); // Set name using useEffect useEffect(()=>{ if(location.state === null) setName(props.name);.. 2023. 3. 16.
3. Spread Operator (Managing Array State) 2023.03.02 0. Background The reason that made me search for and use it is basically that I wanted to manage a state of an array through useState hook. It didn't work when I tried just updating the array with array functions like this: import {useState} from 'react'; const [tags, setTags] = useState([]); const setTags = (tag) => { setTags(tags.push(tag)); }; const removeTags = () => { setTags(tag.. 2023. 3. 2.
2. Connect Node.js to React.js with Express and Mysql 0. Background Since none of my knowledge has anything to do with backend, I've been searching up how to connect Node.js to React.js for a quite long time.. (maybe being lazy took most of it tho). Anyway, I am making notes of how the connection has been made in this project. 1. Download Downloading is the easiest part. npm install express npm install mysql I wasn't sure cors is required but most .. 2023. 1. 2.