๐ŸŠ ๋‚‘๊นกํŒœ_05 : @emotion/styled - Passing Props

Boriยท2022๋…„ 1์›” 22์ผ
1

Project_Kkingkkang.farm

๋ชฉ๋ก ๋ณด๊ธฐ
5/9
post-thumbnail
post-custom-banner

์ง„ํ–‰ ์ƒํ™ฉ

edit-profile.tsx

  • my-page์—์„œ ํ”„๋กœํ•„ ์ˆ˜์ • ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ํ”„๋กœํ•„์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€
  • ์ž…๋ ฅ์ฐฝ์— ๋Œ€ํ•œ ๋ช…์„ธ๋Š” ํšŒ์›๊ฐ€์ž…์˜ ํ”„๋กœํ•„ ์„ค์ •๊ณผ ๋™์ผ

add-product.tsx

  • my-page์—์„œ ์ƒํ’ˆ ๋“ฑ๋ก ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ์ƒํ’ˆ ๋“ฑ๋ก ํŽ˜์ด์ง€

post.tsx

  • ๊ฒŒ์‹œ๊ธ€ ํ•˜๋‹จ์— ๋งํ’์„  ์•„์ด์ฝ˜์„ ํด๋ฆญํ•˜๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ๋Œ“๊ธ€์„ ํ™•์ธํ•˜๊ณ  ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€

upload.tsx

  • ํ•˜๋‹จ์˜ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ”์—์„œ ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ์„ ํด๋ฆญํ•˜๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ ํŽ˜์ด์ง€

list-chat.tsx

  • ํ•˜๋‹จ์˜ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ”์—์„œ ์ฑ„ํŒ…์„ ํด๋ฆญํ•˜๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ์ฑ„ํŒ… ๋ชฉ๋ก ํŽ˜์ด์ง€
  • ์ฝ์ง€ ์•Š์€ ๋ฉ”์‹œ์ง€๊ฐ€ ์žˆ๋Š” ์ฑ„ํŒ…๋ฐฉ์ธ ๊ฒฝ์šฐ ํ”„๋กœํ•„ ์‚ฌ์ง„ ์ขŒ์ธก ์ƒ๋‹จ์— ์ž‘์€ ์›์œผ๋กœ ํ‘œ์‹œ

UserAvatar ์ปดํฌ๋„ŒํŠธ

  • ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์œ ์ € ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๋ฅผ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค๊ธฐ
    • ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ ํŽ˜์ด์ง€์— ๋”ฐ๋กœ ์ ์šฉ
      => ๋งŽ์€ ๊ณณ์— ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๊ฐ€ ์‚ฌ์šฉ๋˜๊ณ , ์ด๋ฏธ์ง€ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๋‹ค.
    • ๊ฐ ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ
      => ์ด๋ฏธ์ง€ ํฌ๊ธฐ 42โ…น42, 50โ…น50, 110โ…น110 ์œผ๋กœ ๋”ฐ๋กœ ๋งŒ๋“ฆ
    • ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ํ•„์š”ํ•œ ๊ฐ’์„ props๋กœ ์ „๋‹ฌํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •
// UserAvatar.tsx
import styled from '@emotion/styled';
import { COLORS } from '../constants';

type AvatarProps = {
  size: string;
  padding: string;
};

export const UserAvatar = ({ size, padding }: AvatarProps) => {
  return (
    <ImgProfile
      size={size}
      padding={padding}
      src="/default-profile-w.png"
      alt="์‚ฌ์šฉ์ž ํ”„๋กœํ•„ ์ด๋ฏธ์ง€"
    />
  );
};

const ImgProfile = styled.img<AvatarProps>`
  width: ${(props) => props.size};
  height: ${(props) => props.size};
  padding: ${(props) => props.padding};
  border-radius: 50%;
  background-color: ${COLORS.light_gray};
`;
  • ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ์„ ํ•˜๋Š” ์ค‘์ธ๋ฐ, ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ ์šฉํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ๊ฐ size์™€ padding ๊ฐ’์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ  ๊ทธ๋Ÿฌ๋ฉด์„œ ๊ฐ’์ด ์ž˜๋ชป ์ ์šฉ๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์„ ์‹ฌ์‹ฌ์น˜ ์•Š๊ฒŒ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค...
    => ์ˆ˜์ • ํ•„์š”^^
    => ์ˆ˜์ •ํ•จ!
<UserAvatar size="30px" padding="6px" />
<UserAvatar size="40px" padding="5px" />
<UserAvatar size="90px" padding="10px" />

์ฝ”๋“œ ์ˆ˜์ •!

  • constants ๋””๋ ‰ํ† ๋ฆฌ์˜ index.ts ์— USER_AVATAR ์ถ”๊ฐ€
export const USER_AVATAR = {
  xs: {
    size: '24px',
    padding: '6px',
  },
  sm: {
    size: '30px',
    padding: '6px',
  },
  md: {
    size: '40px',
    padding: '5px',
  },
  lg: {
    size: '90px',
    padding: '10px',
  },
};
  • ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉ
    => ์ด ์ „ ์ฝ”๋“œ์—์„œ๋Š” ๋ชจ๋“  UserAvata ์ปดํฌ๋„ŒํŠธ์˜ size์™€ padding์— ๊ฐ’์„ ์ง์ ‘ ์ž…๋ ฅ
    => ์ฝ”๋“œ ์ˆ˜์ • ํ›„ constanst์— ๊ฐ ์‚ฌ์ด์ฆˆ์— ๋Œ€ํ•œ ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ €์žฅํ•ด๋†“๊ณ  ๊ทธ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉ
    => ์‹ค์ˆ˜๋ฅผ ์ค„์ด๊ณ , ๊ฐ’ ์ˆ˜์ •์ด ์šฉ์ดํ•ด์ง
<UserAvatar size={USER_AVATAR.xs.size} padding={USER_AVATAR.xs.padding} />
<UserAvatar size={USER_AVATAR.sm.size} padding={USER_AVATAR.sm.padding} />
<UserAvatar size={USER_AVATAR.md.size} padding={USER_AVATAR.md.padding} />
<UserAvatar size={USER_AVATAR.lg.size} padding={USER_AVATAR.lg.padding} />  

์ฐธ๊ณ ๋งํฌ

@emotion/styled - Passing Props
=> props ์ „๋‹ฌ ๋ฐฉ๋ฒ•์„ ๊ตฌ๊ธ€๋ง ํ–ˆ์œผ๋‚˜ ๊ฒฐ๊ตญ์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์•˜๋‹ค! (๋ฟŒ๋“ฏ)

๋งˆ๋ฌด๋ฆฌ

  • ๊ณต์‹๋ฌธ์„œ ๋ณด๋Š” ๊ฑฐ ์‹ซ์–ดํ–ˆ๋Š”๋ฐ.. ์ด์   ์กฐ๊ธˆ ๊ดœ์ฐฎ
  • ํŽ˜์ด์ง€ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๊ณ„์† ์ปดํฌ๋„ŒํŠธํ™” ํ•˜๋Š” ์ค‘์ด๋‹ค.
    ์ฒ˜์Œ์—๋Š” ์–ด๋Š ๋ถ€๋ถ„์„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”์ง€ ์ž˜ ๋ชฐ๋ž๋Š”๋ฐ, ์ผ๋‹จ ์ฝ”๋“œ ์ž‘์„ฑ์„ ํ•˜๋‹ค๋ณด๋ฉด ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„์ด ๊ทธ์ œ์„œ์•ผ ๋ณด์ธ๋‹ค.
    => ๊ทธ๋ฆฌ๊ณ  ๋Š์ž„์—†์ด ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ...
    => ์ฝ”๋“œ๊ฐ€ ์ •๋ฆฌ๋˜๋Š” ๊ฑธ ๋ณด๋ฉด ๋ฟŒ๋“ฏํ•ด์ง„๋‹ค์•„
post-custom-banner

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 1์›” 25์ผ

์ƒˆ ํ‹ธ ์ž‘์„ฑ ์–ธ์ œ ํ•˜๋งจ๋งˆ์”ธ

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ