Home ๐ŸŠ 2022-08-11
Post
Cancel

๐ŸŠ 2022-08-11

๐Ÿ”ฅ ์˜ค๋Š˜ ์–ด๋””์— ๋„์ „ํ•ด๋ณด์…จ๊ณ , ๋ฌด์—‡์„ ๋ฐฐ์šฐ์…จ๋‚˜์š”?

๐ŸŒŠ ํ•™์Šตํ•˜์‹œ๋ฉด์„œ ๊ถ๊ธˆํ•˜์‹  ๋ถ€๋ถ„์ด๋‚˜, ์–ด๋ ค์› ๋˜ ์ ์€ ๋ญ˜๊นŒ์š”?

  • componentDidUpdate์—์„œ this.setState๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฌดํ•œ ๋ฃจํ”„ ๋ฐœ์ƒ! ๋”ฐ๋ผ์„œ ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ๋ฐฉ์ง€ํ•ด์ค˜์•ผ ํ•œ๋‹ค. setStateInComponentDidUpdate

๐ŸŒŸ ๋‚ด์ผ์€ ์–ด๋–ป๊ฒŒ ํ•ด๋ณด๊ณ  ์‹ถ์œผ์„ธ์š”?


ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช…์ฃผ๊ธฐ

  • componentDidMount

    • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ๋œ ์งํ›„์— ํ˜ธ์ถœ๋œ๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ‰๊ฐ€๋˜๊ณ  DOM์— ๋ Œ๋”๋ง๋˜๋Š” ์‹œ์  useEffect๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. useEffect์— ๋นˆ ์˜์กด์„ฑ ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ฒ˜์Œ ๋ Œ๋”๋ง๋˜๋Š” ์‹œ์ ์—๋งŒ ํ˜ธ์ถœ๋œ๋‹ค. ์ด๋Š” componentDidMount๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • componentDidUpdate

    • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐฑ์‹ ๋˜๋ฉด ํ˜ธ์ถœ, ์˜์กด์„ฑ ๋ฐฐ์—ด์ด ์žˆ๋Š” useEffect์™€ ๋™์ผ

      • ์˜์กด์„ฑ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ํŠน์ • ๊ฐ’์ด ๊ฐฑ์‹ ๋˜์—ˆ์„ ๋•Œ์—๋งŒ ์ƒํƒœ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

      • ์ธ์ž๋กœ (์ด์ „ props, ์ด์ „ state)๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      
      //๐Ÿ‘‡ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์ปดํฌ๋„ŒํŠธ
      class User extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            filteredUsers: DUMMY_USERS,
            searchTerm: "",
          };
        }
      
        componentDidUpdate(prevProps, prevState) {
          // ์ƒํƒœ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ์—๋งŒ setState๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.
          if (prevState.searchTerm !== this.state.searchTerm) {
            this.setState({
              filteredUsers: DUMMY_USERS.filter((user) =>
                user.name.includes(this.state.searchTerm)
              ),
            });
          }
        }
      }
      // ๐Ÿ‘‡ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ
      const User = (props) => {
        const [filteredUsers, setFilteredUsers] = useState(DUMMY_USERS);
        const [searchTerm, setSearchTerm] = useState("");
      
        useEffect(() => {
          setFilteredUsers(
            DUMMY_USERS.filter((user) => user.name.includes(searchTerm))
          );
        }, [searchTerm]);
      };
      
  • componentWillUnmount

    • ์ปดํฌ๋„ŒํŠธ๊ฐ€ DOM์—์„œ ์‚ญ์ œ๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋ฉฐ useEffect์˜ cleanup๊ณผ ๋™์ผ

๐ŸŠ JEJU.log

๐Ÿƒ๐Ÿปโ€โ™€๏ธ ๊น€๋…•์—์„œ ์›”์ •๊นŒ์ง€ ํ•ด์•ˆ๋„๋กœ ๊ฑธ์–ด๊ฐ€๊ธฐ (๋šœ๋ฒ…์ด๋ž€..)

ํ•œ ์‹œ๊ฐ„ ์ •๋„๋ฉด ๊ปŒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ๊ฒฝ์น˜ ๊ตฌ๊ฒฝํ• ๊ฒธ ํ•ด์•ˆ ๋„๋กœ(์˜ฌ๋ ˆ 20๋ฒˆ ์ฝ”์Šค ์ค‘๊ฐ„)๋ฅผ ๊ฑธ์–ด๋ดค๋‹ค. ์‰ฌ์šธ์ค„ ์•Œ์•˜๋Š”๋ฐ ์ œ์ฃผ๋Š” ๋‚ ์ด ๋„ˆ๋ฌด ๋„ˆ๋ฌด ๋”์›Œ์„œ ์ •๋ง์ด์ง€ ๋„ˆ๋ฌด ํž˜๋“ค์—ˆ๋‹ค. ์„ ํฌ๋ฆผ๋„ ์•ˆ๋ฐœ๋ผ์„œ ์–ผ๊ตด์ด๋ฉฐ ํŒ”์ด๋ฉฐ ๋‹ค ๋นจ๊ฐ›๊ฒŒ ๋˜๊ณ  ๋•€์„ ๋ป˜๋ป˜ ํ˜๋ ธ๋‹ค. ์™œ ์•„๋ฌด๋„ ์•ˆ๊ฑธ์–ด๊ฐ€๋Š”์ง€ ์•Œ ๊ฑฐ ๊ฐ™์•˜๋‹ค.. ๊ทธ๋ž˜๋„ ๋‚ ์ด ๋ง‘์•„์„œ ๋ฐ”๋‹ค๊ฐ€ ๋„ˆ๋ฌด ์˜ˆ๋ปค๋‹ค.

gimnyeong woljeong

This post is licensed under CC BY 4.0 by the author.

๐ŸŠ 2022-08-09

๐ŸŠ 2022-08-16