ํ์ด์ค ๊ฐ๋ฐ์ ์ฌ์ฉํ ๊ธฐ์
๊ณต๊ฐ ์ํํธ์จ์ด
ํ๋ฉด ์ถ๋ ฅ์ ํนํ๋ ํ๋ ์์ํฌ
์ปดํฌ๋ํธ๋ฅผ ์กฐ๋ฆฝํ์ฌ ํ๋ฉด ๊ตฌ์ฑ
๊ฒ์ ์์ง ์๋ฆฌ๋ฅผ ๋์ ํ์ฌ ํ๋ฉด ์ถ๋ ฅ ์๋๊ฐ ๋น ๋ฆ(Virtual Dom)
function App() {
let counter = 0;
const increase = ()=>{
counter = counter + 1;
}
return (
<div>
<Box1 name="ํ๊ตญ"/>
<Box1 name="๋ฏธ๊ตญ"/>
<Box1 name="์ค๊ตญ"/>
<div>{counter}</div>
<button onClick={increase}>ํด๋ฆญ!</button>
</div>
);
}
โข npx create-react-app ํ๋ก์ ํธ ๋๋ ํฐ๋ฆฌ
โข cd ํ๋ก์ ํธ ๋๋ ํฐ๋ฆฌ
โข npx โp storybook sb init
โข npm run storybook
โข main.js : stories๋ฅผ ์ํ config ์ค์
โข preview.js : ๋ชจ๋ story๋ค์ ๊ธ๋ก๋ฒํ๊ฒ ์ ์ฉ๋ ํฌ๋งท ์ธํ
Export default {
Title : ์คํ ๋ฆฌ๋ถ์ ์ฌ๋ฆด component ํด๋ ๊ณ์ธต ๊ตฌ์กฐ,
Component : ์คํ ๋ฆฌ๋ฅผ ๋ง๋ค ์ปดํฌ๋ํธ ์ด๋ฆ
}
Export const ์คํ ๋ฆฌ์ด๋ฆ = () => ํด๋น์คํ ๋ฆฌ์์ ํ ์คํธํ ์ธ์๊ฐ ๋ด๊ธด ์ปดํฌ๋ํธ
import React, { Component } from 'react'
import PropTypes from "prop-types" ;
export function Text({children, color, italic, underline}) {
const style = {
color : color,
fontStyle : italic? "italic" : "normal",
textDecoration : underline? "underline" : "none"
};
return <span style={style}>{children}</span>
};
Text.propTypes = {
children: PropTypes.string.isRequired,
color : PropTypes.string,
italic : PropTypes.bool,
underline : PropTypes.bool,
};
Text.defaultProps = {
color : "black",
italic : false,
underline : false,
}
import React, {Component} from "react";
import {Text} from "./Text";
export default {
title : "Text",
component : Text,
};
const TEST_TEXT = "Story Text Test";
export const Default = ()=><Text>{TEST_TEXT}</Text>;
export const Red = ()=><Text color="red">{TEST_TEXT}</Text>;
export const Italic = ()=><Text italic>{TEST_TEXT}</Text>;
export const Underline = ()=><Text underline>{TEST_TEXT}</Text>;
Storybook์์ ํ์ธํ ์ ์์
import React, { Component } from "react";
import PropTypes from "prop-types";
class Input extends Component {
constructor(props) {
super(props);
this.setRef = this.setRef.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
const { name, onChange } = this.props;
if (onChange) {
onChange(name, e.target.value);
}
}
componentDidMount() {
if (this.props.autoFocus) {
this.ref.focus();
}
}
componentDidUpdate() {
if (this.props.autoFocus) {
this.ref.focus();
}
}
setRef(ref) {
this.ref = ref;
}
render() {
const { errorMessage, label, name, value, type, onFocus } = this.props;
return (
<label>
{label}
<input
id={"input_${name}"}
ref={this.setRef}
onChange={this.handleChange}
onFocus={onFocus}
value={value}
type={type}
/>
{errorMessage && <span className="error">{errorMessage}</span>}
</label>
);
}
}
Input.propTypes = {
type: PropTypes.oneOf(["text", "number", "price"]),
name: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
errorMessage: PropTypes.string,
label: PropTypes.string,
onChange: PropTypes.func,
autoFocus: PropTypes.bool,
};
Input.defaultProps = {
onChange: () => {},
onFocus: () => {},
autoFocus: false,
type: "text",
};
export default Input;
import React, { Component } from 'react';
import Input from "./Input";
export default{
title : "Input",
component : Input,
};
export const label = ()=><Input name="name" label="์ด๋ฆ :"></Input>;
์์ํ ๋ด์ฉ์ด๋ผ์ ์ด๋ ต๊ฒ ๋๊ปด์ง.
โ๏ธ ์ด๋ป๊ฒ ํด๊ฒฐ์ ํ๋๊ฐ?
โ๏ธ ์ด๋ ๊ฒ ์ดํด๋ฅผ ํ๋ค
โ๏ธ ์ด๋๊น์ง ์ดํดํ์ง?
โ๏ธ ๋ค์์ ์๋ํด๋ณผ ๋ฐฉ๋ฒ
์ ์ฒด ๊ณผ์ ๊ณผ ์ฝ๋๋ฅผ ๋ค์ ๋ณด๊ณ ์ดํดํ๋ ์๊ฐ์ ๊ฐ์ ธ์ผ๊ฒ ๋ค
Storybook์ ๋ํ ๋ด์ฉ์ ๋ค์ ํ๋ฒ ๋ณต์ตํ๋ ์๊ฐ์ด ํ์ํ ๊ฒ ๊ฐ๋ค