import React, { Component } from 'react';
class Box1 extends Component {
render(props) {
return (
<div className="box">
Box{this.props.index}
<p>{this.props.name}</p>
</div>
);
}
}
export default Box1;
css code
.box {
width: 100px;
height: 100px;
border: 1px solid red;
}
import logo from './logo.svg';
import './App.css';
import Box1 from './component/Box1';
import {useState} from "react";
function App() {
// let counter = 0;
const [counter, setCounter] = useState(0);
const [counter2, setCounter2] = useState(0);
const increase = () => {
setCounter(counter + 1);
// console.log("counter๋ ", counter);
}
const decrease = () => {
setCounter(counter - 1);
// console.log("counter๋ ", counter);
}
return (
<div>
<Box1 name="ํ๊ตญ" index="1"/>
<Box1 name="๋ฏธ๊ตญ" index="2"/>
<Box1 name="์ค๊ตญ" index="3"/>
<div>{counter}</div>
<button onClick={increase}>์ฆ๊ฐ!</button>
<button onClick={decrease}>๊ฐ์!</button>
</div>
);
}
exprot default App;
- stroybook์ ์ค์นํ๊ธฐ ์ํด ํ๋ก์ ํธ ์์ฑํ๊ธฐ
- npx -p storybook sb init ์ ์ฌ์ฉํ์ฌ ํด๋์์ ์ด๊ธฐํ์์ผ์ฃผ๊ธฐ
- npm run storybook ์ผ๋ก ์คํํ๊ธฐ
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>;
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="์ด๋ฆ : "/>;
๋ฆฌ์ํธ์๋ํ์ฌ ๋ ๊ณต๋ถ๊ฐ ํ์ํ๋ค๊ณ ์๊ฐ์ด๋ค์๋ค.