๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ‘จ‍๐Ÿ’ป web.dev/fe

Storybook useArgs ํ™œ์šฉํ•˜๊ธฐ

by HandHand 2023. 8. 28.

 

๐Ÿ“Œ ์˜ˆ์‹œ ์‚ดํŽด๋ณด๊ธฐ

์Šคํ† ๋ฆฌ๋ถ์„ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋ฉด control prop์„ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์™€ ๋™๊ธฐํ™”์‹œํ‚ค๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ๋ฅผ ์œ„ํ•ด ์ง€๋„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๊ณ , ๋งˆ์ปค ํด๋ฆญ ์‹œ ํ™œ์„ฑํ™”๋˜๋Š” ๋งˆ์ปค ID ๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์Šคํ† ๋ฆฌ๋ฅผ ๊ฐ€์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

import { Map } from './map'

const meta = {
    component: Map
}

export default meta

const mock = [
    { id: '1', poi: {/** some data */} },
    { id: '2', poi: {/** some data */} },
    { id: '3', poi: {/** some data */} },
    // ...
]

const options = mock.map((option) => option.id)

export const BasicStory = {
    render: (args) => <Map {...args} />,
    argTypes: {
        activeId: {
            control: 'select',
            options,
        }
    },
    args: {
        activeId: options[0],
        pois: mock,
    }
}

 

์‹คํ–‰ํ•ด๋ณด๋ฉด control panel ์—์„œ activeId ๋ฅผ ๋ณ€๊ฒฝํ• ๋•Œ์™€ ๋‹ฌ๋ฆฌ ์ง€๋„ ๋งˆ์ปค๋ฅผ ์ง์ ‘ ํด๋ฆญํ•  ๊ฒฝ์šฐ์—๋Š” activeId args๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ณ  Map ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ’๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“Œ useArgs ํ™œ์šฉํ•˜๊ธฐ

์Šคํ† ๋ฆฌ๋ถ์—์„œ ์ œ๊ณตํ•˜๋Š” useArgs Hook ์„ ์‚ฌ์šฉํ•˜๋ฉด args ๋ฅผ ๋™๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ Œ๋”๋งํ•  ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ์‹ธ๋Š” Story ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  useArgs ๋กœ arg ๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

 

const Story = (args) => {
        const [{ activeId }, updateArgs] = useArgs()

        const handleSelect = (id) => updateArgs({ activeId: id })

        return <Map {...args} activeId={activeId} onSelect={handleSelect} />
}

const BasicStory = {
    render: Story,
    // ...
}

 

๐Ÿ“Œ ์ ์šฉ ๊ฒฐ๊ณผ

 

 

์ด์ œ ํด๋ฆญ ์ด๋ฒคํŠธ์— ๋”ฐ๋ผ ์„ ํƒ๋œ ์žฅ์†Œ์˜ ID ์— ๋งž์ถฐ activeId ๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋˜๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ ์ฐธ๊ณ ์ž๋ฃŒ

Changing args values / state in Storybook (without useState hook)

๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€