๋ฐ˜์‘ํ˜•

Framework/React 47

[React] recharts chart ๋„“์ด 100% ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• (+ typescript)

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ €๋Š” recharts chart ์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฐจํŠธ์˜ ๋„“์ด๋ฅผ ํ™”๋ฉด 100%์— ๋งž์ถฐ์„œ ๋ฟŒ๋ ค์ฃผ๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ์š”. width ๊ฐ’์„ 100%๋กœ ์„ค์ •ํ•˜์ž ๋‹ค์Œ์˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. TS2322: Type 'string' is not assignable to type 'number'. import {NextPage} from "next"; import React from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts'; const data = [ { name: 'Jan', uv: 4000, pv: 2400, amt: 2400, }, { name: 'Feb'..

Framework/React 2023.04.29

[React] Zustand ์„ค์น˜ ๋ฐฉ๋ฒ• ๋ฐ ์˜ˆ์ œ ์‚ดํŽด๋ณด๊ธฐ

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Zustand ์„ค์น˜ ๋ฐฉ๋ฒ• ๋ฐ ์˜ˆ์ก”๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ์ „์— ์•ž์„œ zustand ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์œ„ ํฌ์ŠคํŒ… ์ฐธ๊ณ  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค :) How to install Zustand Zustand ์„ค์น˜๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ์ž ์‚ฌ์šฉํ•˜๋Š” ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ ํˆด(npm / yarn)์— ๋งž์ถฐ ์„ค์น˜ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 1. npm npm install zustand 2. yarn yarn add zustand Zustand Simple Example ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋กœ ์นด์šดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. zustand๋ฅผ importํ•˜๊ณ , ์ƒํƒœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ ์•ˆ์—๋Š” count, increment(), decrement()์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. import create fro..

Framework/React 2023.04.17

[React] Zustand๋ž€?

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” React Zustand ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Zustand ๋ฆฌ์•กํŠธ๋ฅผ ๊ณต๋ถ€ํ•˜์‹  ๋ถ„์ด๋ผ๋ฉด, ์ƒํƒœ (State)๋ผ๋Š” ๊ฐœ๋…์— ๋Œ€ํ•ด ๋“ค์–ด๋ณด์…จ์„ ๊ฒ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•˜์ž๋ฉด React Zustand๋Š” ๋ฆฌ์•กํŠธ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๋ฆฌ์•กํŠธ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Redux, MobX ๋“ฑ์ด ์กด์žฌํ•˜์ง€๋งŒ Zustand๋Š” ์œ„ ๋ผ์ด๋ธŒ๋Ÿฌ์— ๋น„ํ•ด ๋” ๊ฐ€๋ณ๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Zustand Features Zustand๋Š” State๋ฅผ ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉฐ ์ปดํฌ๋„ŒํŠธ ๊ฐ„์— ๊ณต์œ ๋˜๋Š” State Container๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด Container ๋Š” ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ useStore()์„ ํ†ตํ•ด ์ƒํƒœ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  (get), ์—…๋ฐ์ด..

Framework/React 2023.04.14

[React] ๋ฆฌ์•กํŠธ innerHtml ํ‘œ์‹œํ•˜๊ธฐ (dangerouslySetInnerHTML)์‚ฌ์šฉ ์ฃผ์˜์  ๋ฐ ๋ณด์•ˆ ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ๋ฆฌ์•กํŠธ์—์„œ innerHtml์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ์Šค๋งˆํŠธ์—๋””ํ„ฐ ๊ด€๋ จํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์—๋””ํ„ฐ์— ์ €์žฅ๋œ ๋‚ด์šฉ์ด ๊ทธ๋Œ€๋กœ html tag๋กœ ์ €์žฅ๋˜๊ณ , ์ €์žฅ๋œ html tag๋ฅผ ๊ทธ๋Œ€๋กœ ํ™”๋ฉด์— ํ‘œ์‹œํ•ด์•ผ ํ–ˆ์ฃ . ์ €์žฅ๋œ ์ด innerHtml๋ฅผ ์–ด๋–ป๊ฒŒ ํ‘œ์‹œํ•ด์•ผ ํ• ๊นŒ์š”? ํ•จ๊ป˜ ์•Œ์•„๋ด…์‹œ๋‹ค :) How to solve the problem React์—์„œ innerHtml๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด dangerouslySetInnerHTML๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import React from 'react'; function MyComponent() { const sampleHtml = 'Hello world! '; return ( ); } ์—ฌ๊ธฐ์„œ da..

Framework/React 2023.04.06

[React] ๋กœ๋”ฉ๋ฐ”(loading bar) ํ‘œ์‹œ ๋ฐ ๋กœ๋”ฉ ์†๋„ ์กฐ์ ˆํ•˜๋Š” ๋ฐฉ๋ฒ• (+ setTimeout)

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฆฌ์•กํŠธ์—์„œ ๋กœ๋”ฉ๋ฐ” ํ‘œ์‹œ ๋ฐ ๋กœ๋”ฉ ์†๋„๋ฅผ ์กฐ์ ˆํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. How to solve the problem ๋‹ค์Œ์˜ List ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. handleFilterClickํ•จ์ˆ˜๋ฅผ ํด๋ฆญ ํ• ๋•Œ๋งˆ๋‹ค ํ•„ํ„ฐ๊ธฐ๋Šฅ์ด ์ž‘๋™๋˜๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค. ํ•„ํ„ฐ๋ฅผ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ํ•„ํ„ฐ๊ฐ€ ๋œ๋“ฏํ•œ ๋™์ž‘์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋กœ๋”ฉ๋ฐ”๋ฅผ ์ถ”๊ฐ€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. const List: NextPage = () => { const {dataList, setDataList} = useLoginStore(); const [totalCtn, setTotalCtn] = useState(); const [filterStatus, setFilterStatus] = useState("ALL"); const..

Framework/React 2023.04.05

[React] array data ๋งˆ์ง€๋ง‰ ์ž์‹ ์š”์†Œ์— border bottom line css ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฆฌ์•กํŠธ์—์„œ array ๋ฐ์ดํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ์š”์†Œ์— border bottom line ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. How to solve the problem 1. map ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค. const arrayLength = array.length; 2. map ํ•จ์ˆ˜์—์„œ ํ˜„์žฌ ๋ฐ์ดํ„ฐ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ฐ›์•„ ์˜ต๋‹ˆ๋‹ค. array.map((item, index) => { //... }) 3. ์ธ๋ฑ์Šค ๊ฐ’์ด ๋ฐฐ์—ด ๊ธธ์ด๋ณด๋‹ค 1 ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” border bottom์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค array.map((item, index) => { const isLastItem = index === arrayLength - 1; return ( {item}..

Framework/React 2023.03.31

[React] typescript useRef ์‚ฌ์šฉ์‹œ undefined / null ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์›์ธ ๋ฐ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” react ์—์„œ useRef ์‚ฌ์šฉ์‹œ undefined / null ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์›์ธ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ œ๊ฐ€ useRef๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ ์˜ˆ์ œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. clientHeight๋ฅผ ์กฐ์ ˆํ•˜๊ธฐ ์œ„ํ•ด useRef๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ div tag์— ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. const divRef = useRef(null); const [handlePosition, setHandlePosition] = useState('300vh'); useEffect(() => { setHandlePosition(`${(divRef.current.clientHeight * 3) / 300}px`); }, []); return ( {/* some content */} ); ํ•˜์ง€..

Framework/React 2023.03.29

[React] ๋ฆฌ์•กํŠธ apexcharts x์ถ•(xaxis) ํŠน์ • ๊ฐ’์œผ๋กœ ์‹œ์ž‘ํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฆฌ์•กํŠธ apexcharts x์ถ• ๊ฐ’์„ ํŠน์ • ๊ฐ’์œผ๋กœ ์‹œ์ž‘ํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. How to solve the problem ์ œ ๊ฒฝ์šฐ์—๋Š” ์ƒˆ๋ฒฝ 5์‹œ๋ผ๋Š” ํŠน์ • ๊ฐ’์œผ๋กœ x์ถ•์„ ์‹œ์ž‘ํ•˜๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ €๋Š” apxechart xaxis์˜ min ์†์„ฑ์„ ์‚ฌ์šฉํ•ด ์ œ๊ฐ€ ์›ํ•˜๋Š” ์‹œ๊ฐ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. x์ถ• ๊ฐ’์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅผํ…Œ๋‹ˆ ๋…„๋„/๋‹ฌ/์ผ ๋“ฑ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์ปค์Šคํ…€ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. xaxis: { min: new Date(new Date().setHours(5, 0, 0, 0)).getTime(), } ์•„๋ž˜๋Š” ์ „์ฒด ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. var options = { chart: { type: 'line' }, series: [{ name: ..

Framework/React 2023.03.28

[React] ๋ฆฌ์•กํŠธ ํˆดํŒ (Tooltip) ๊ตฌํ˜„ํ•˜๊ธฐ / ์™ธ๋ถ€์˜์—ญ ํด๋ฆญ ์‹œ ํˆดํŒ ์ˆจ๊ธฐ๋Š” ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ์‹œ๊ฐ„์—๋Š” ๋ฆฌ์•กํŠธ์—์„œ ํˆดํŒ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•, ํˆดํŒ ์™ธ๋ถ€ ์˜์—ญ ํด๋ฆญ ์‹œ ํˆดํŒ์„ ์ˆจ๊ธฐ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Let's look at detail ์•„๋ž˜์™€ ๊ฐ™์€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. 3๊ฐœ์˜ ํˆดํŒ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๊ณ , ํˆดํŒ ์ปดํฌ๋„ŒํŠธ์—๋Š” ๊ฐ๊ธฐ ๋‹ค๋ฅธ ํˆดํŒ ์„ค๋ช…์„ props๋กœ ์ „๋‹ฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. const SomeComponent: NextPage = props => { return ( ๋‰ด์Šค ์‡ผํ•‘ ์ง€๋„ ); } export default SomeComponent; ํˆดํŒ ์ปดํฌ๋„ŒํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์žฌ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ํˆดํŒ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค, ์„ ํƒ๋œ ํˆดํŒ์ด ์žˆ์„ ๋•Œ ๋‹ค๋ฅธ ํˆดํŒ์ด ์„ ํƒ๋˜์–ด๋„, ๋™์‹œ์— ํˆดํŒ desc๊ฐ€ ํ‘œ์ถœ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์™ธ๋ถ€ ์˜์—ญ์„ ํด๋ฆญ ํ• ๋•Œ ์—ฌ..

Framework/React 2023.03.21

[React] ๋ฆฌ์•กํŠธ apexcharts ์ปค์Šคํ…€ ํˆดํŒ (custom tooltip) ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•, ํˆดํŒ์— ์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ์™€ ๊ฐ’ ํ‘œ์‹œํ•˜๊ธฐ

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ์‹œ๊ฐ„์—๋Š” ๋ฆฌ์•กํŠธ์—์„œ apexcharts ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปค์Šคํ…€ ํˆดํŒ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. How to solve the problem apexcharts ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์—, ์•„๋ž˜์˜ series ๋ฐ์ดํ„ฐ์™€ chart option์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค. const series: any = [ { name: 'EBAY', data: [ ['2023-03-20T07:00:22.625Z', '123124.82'], ['2023-03-20T06:00:20.103Z', '52353.83'], ['2023-03-20T05:00:16.960Z', '98943.91'], ['2023-03-20T04:00:15.051Z', '796756.65'], ['2023-03-20T0..

Framework/React 2023.03.20
๋ฐ˜์‘ํ˜•