값을 바꾸고자 할때 state 사용
function App() {
let counter = 0;
const increase =()=>{
conter = conter + 1;
};
return (
<div>
<div>{counter}</div>
<button onClick={increase}>클릭!</button>
</div>
);
}

import {useState} from "react";
function App() {
const [counter, setCounter] = useState(0);
const increase =()=>{
setCounter(counter +1);
};
const decrease =()=>{
setCounter(counter -1);
};
return (
<div>
<div>{counter}</div>
<button onClick={increase}>증가 버튼</button>
<button onClick={decrease}>감소 버튼</button>
</div>
);
}







import React, { Component } from 'react';
import PropTypes from "prop-types";
// chlidren 객체를 받아서 색, 글씨체, 밑줄을 적용해 볼 것이다.
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이나 underline은 참, 거짓만 있으면 되니 bool
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>;
Export default {
Title : 스토리북에 올릴 component 폴더 계층 구조,
Component : 스토리를 만들 컴포넌트 이름
}
Export const 스토리이름 = () => 해당스토리에서 테스트할 인자가 담긴 컴
포넌트


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;
2.스토리북에 연결(등록)
import React, { Component } from 'react';
import Input from "./Input";
export default{
title : "Input",
component : Input,
};
export const label = () => <Input name="name" label="이름 : "/>;

스토리북를 쓰는 이유와 장점 및 실습을 해보았는데, 처음 부분은 상당히 어려웠다. 명령어라던지, 생성 방법 및 적용에 접근이 어려움이 있었다. 함수 자체도 아직까지는 어떤 것을 사용해야 하는지 어렵다. 오늘 배운 text, input 컴포넌트 뿐만 아니라 다른 종류도 공부해봐야 겠다.