Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[03/13] ํ›… #95

Open
dxwwan opened this issue Mar 12, 2024 · 0 comments
Open

[03/13] ํ›… #95

dxwwan opened this issue Mar 12, 2024 · 0 comments
Assignees

Comments

@dxwwan
Copy link
Collaborator

dxwwan commented Mar 12, 2024

๐Ÿชํ›…

  • ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋Š” ์ƒํƒœ ๋กœ์ง์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

1. ๋ฆฌ์•กํŠธ ํ›…

  • ํ›…์ด ์ƒ๊ธฐ๊ธฐ ์ „์—๋Š” ๋””๋ฒ„๊น…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ์ƒ๋ช…์ฃผ๊ธฐ์˜ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋น„๊ตํ–ˆ์–ด์•ผ ํ–ˆ๋‹ค.
  • ํ›…์ด ๋„์ž…๋˜๋ฉด์„œ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ์— ๋งž์ถฐ ๋กœ์ง์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

useState

  • ํŠœํ”Œ์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ : ์ œ๋„ค๋ฆญ์œผ๋กœ ์ง€์ •ํ•œ S ํƒ€์ž…

  • ํŠœํ”Œ์˜ ๋‘๋ฒˆ์งธ ์š”์†Œ : ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋Š” Dispatch ํƒ€์ž…์˜ ํ•จ์ˆ˜

  • ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ›…

    import { useState } from "react";
    
    const MemberList = () => {
    	const [memberList, setMemberList] = useState([
    		{
    			name: "KingBaedal",
    			age: 10,
    		},
    		{
    			name: "MayBaedal",
    			age: 9,
    		},
    	]);
    	const sumAge = memberList.reduce((sum, member) => sum + member.age, 0);
    	
    	const addMember = () => {
    		setMemberList([
    			...memberList,
    			{
    				name: "DokgoBaedal",
    				age: 11,
    			},
    		]);
    	}	;
    };
    interface Member {
    	name: string;
    	age: number;
    }
    • memberList์— Member์ƒํƒœ๋ฅผ ๋ถ€์—ฌํ•ด์คŒ์œผ๋กœ์จ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์˜์กด์„ฑ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ํ›…

    1. useEffect ( EffectCallback, deps )

      • ๋ Œ๋”๋ง ์ดํ›„์— ๋ฆฌ์•กํŠธ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์— ์–ด๋–ค ์ผ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์คŒ

      • ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” Promiseํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋น„๋™๊ธฐ ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๋‹ค.

      • ๊ฒฝ์Ÿ ์ƒํƒœ๋ฅผ ๋ง‰๊ธฐ์œ„ํ•จ

      • ๋‘๋ฒˆ์งธ ์ธ์ž์ธ deps๋Š” effect๊ฐ€ ์ˆ˜ํ–‰๋˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์„ ๋‚˜์—ดํ•œ๋‹ค.

        • deps ๋ฐฐ์—ด์˜ ์›์†Œ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์‹คํ–‰๋œ๋‹ค๋Š” ์‹์œผ๋กœ ์‚ฌ์šฉ
      • useEffect๋Š” deps์˜ ๋ณ€๊ฒฝ์„ ์–•์€ ๋น„๊ต๋กœ๋งŒ ํŒ๋‹จํ•˜๊ธฐ์— ๋ถ€๋ชจ์—์„œ ๋ฐ›์€ ์ธ์ž๋ฅผ deps๋กœ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ ์ฒ˜๋Ÿผ ๊ฐ์ฒด์˜ ์ฐธ์กฐ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค

      • ์˜๋„์น˜ ์•Š๋Š” ์žฌ๋žœ๋”๋ง์„ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

        const [name, setName] = useState("");
        
        useEffect(() => {
        	setName("๋ฐฐ๋‹ฌ์ด")
        },[]);
        
        return (
        	<div>
        		{`์•ˆ๋…•ํ•˜์„ธ์š”, ${name}๋‹˜!`}
        	</div>
        );
        
        //์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋œ ํ›„, name์ด ๋นˆ์นธ์œผ๋กœ ๋ Œ๋”๋ง
        //setName์ด ์™„๋ฃŒ ๋  ๋•Œ ๊นŒ์ง€ ๋นˆ์นธ ์œ ์ง€
    2. useLayoutEffect

      • ์ •์˜๋Š” useEffect์™€ ๋™์ผํ•˜๋ฉฐ ํ•˜๋Š” ์—ญํ• ์˜ ์ฐจ์ด๋งŒ ์žˆ๋‹ค.
      • useLayoutEffect๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ™”๋ฉด์— ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ทธ๋ ค์ง€๊ธฐ ์ „์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ ๋ฒˆ์งธ ๋ Œ๋”๋ง ๋•Œ ๋นˆ ์ด๋ฆ„์ด ๋œจ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
    3. useMemo, useCallback

      • ์ด์ „์— ์ƒ์„ฑ๋œ ๊ฐ’ ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ธฐ์–ตํ•˜๋ฉฐ, ๋™์ผํ•œ ๊ฐ’๊ณผ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ƒ์„ฑํ•˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.

      • ์–ด๋–ค ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๋•Œ๋‚˜ ๋ Œ๋”๋ง์ด ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” form์—์„œ ์‚ฌ์šฉ

        type DependencyList = ReadonlyArray<any>;
        
        function useMemo<T>(factory: () => T, deps: DependencyList | undefined): T;
        function useCallback<t extends (...args: any[]) => any>(callback: T, deps:DependencyList):T;
        • ์™œ ์ œ๋„ค๋ฆญ์„ ์ง€์›ํ• ๊นŒ?
          • ํ•จ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ
          • ๋‘ ํ›…์€ deps ๋ฐฐ์—ด์„ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ ํ•ด๋‹น ์˜์กด์„ฑ์ด ๋ณ€๊ฒฝ๋˜๋ฉด ๊ฐ’์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋œ๋‹ค.
          • deps ๋ฐฐ์—ด์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•˜๋Š”๋ฐ๋„ ๋‹ค์‹œ ๊ณ„์‚ฐ๋˜์ง€ ์•Š๋„๋ก ์ฃผ์˜
    4. useRef

      • useRef์˜ ์ดˆ๊ธฐ ์„ค์ •๊ฐ’์— null์ด ๋“ค์–ด๊ฐ€๋Š” ์ด์œ 
      • useRef๋Š” MutableRefObject๋‚˜ RefObject๋ฅผ ๋ฐ˜ํ™˜
      • ์ „์ž๋Š” ๊ฐ’์„ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ( null ํฌํ•จ )
      • ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— Ref์ „๋‹ฌ
        • props๋กœ ์ „๋‹ฌํ•˜๋ฉด ์˜ค๋ฅ˜
        • forwardRef๋กœ ์ „๋‹ฌ
          • ForwardRefRenderFunction
      • useRef๋กœ ๊ด€๋ฆฌ๋˜๋Š” ๋ณ€์ˆ˜๋Š” ๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋„ ์ปดํฌ๋„ŒํŠธ์˜ ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • ํ›…์˜ ๊ทœ์น™
      • ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์—์„œ ํ˜ธ์ถœํ•  ๊ฒƒ
      • ์ปดํฌ๋„ŒํŠธ๋‚˜ ์ปค์Šคํ…€ ํ›… ๋“ฑ์˜ ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ๋งŒ ํ˜ธ์ถœ๋  ๊ฒƒ

ISSUE

  • ๋ฆฌ์•กํŠธ์—์„œ ๊ฒฝ์Ÿ ์ƒํƒœ๋กœ ์ธํ•ด ์ผ์–ด๋‚˜๋Š” ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ?
@dxwwan dxwwan self-assigned this Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant