Weitere ähnliche Inhalte
Ähnlich wie Reactのおさらい②.pptx (11)
Mehr von iPride Co., Ltd. (20)
Reactのおさらい②.pptx
- 5. 前回のあらすじ
export const OutputTag = ({list}:{list: string[]}) => {
return <>
<h1>TODO一覧:</h1>
<ul>{list.map((w,i)
=>(<li key={i}>{w}</li>))}</ul>
</>;
}
<OutputTag list={dataList}/>
Componentの使用方法
描画したもの
Componentの定義
- 6. 前回のあらすじ
export const OutputTag = ({list}:{list: string[]}) => {
return <>
<h1>TODO一覧:</h1>
<ul>{list.map((w,i)
=>(<li key={i}>{w}</li>))}</ul>
</>;
}
属性の定義
タグ名の定義
どのようなHTMLを返すか?
Componentの使用方法
- 10. 管理方法 難易度 リロード時 自由度
メモリ上に保持 簡単 消える 高い
URL上 簡単 消えない 低い
Cookie 難しい 消えない 低い
localStorage 普通 消えない 普通
IndexedDB 難しい 消えない 高い
Reactではこちらを
多用する
Web標準における状態管理
- 11. Reactにおける状態管理
export const CountButton = () => {
const [count, setCount] = useState(0);
const add = () => setCount(count + 1);
return <button onClick={add}>
Count: {count}
</button>;
}
Componentの定義の中で、「useState」という関数が使用できる
- 12. Reactにおける状態管理
export const CountButton = () => {
const [count, setCount] = useState(0);
const add = () => setCount(count + 1);
return <button onClick={add}>
Count: {count}
</button>;
}
「const [変数名, 変数を変更するための関数] = useState(初期値);」
といった書式で使う
- 16. const CountButton = () => {
const [count, setCount] = useState(0);
return <LabelButton label={`Count: ${count}`}>;
}
count LabelButton = ({label}:{label: string}) => {
return <button>{label}</button>
}
「状態」のバケツリレー
この場合、CountButtonからLabelButtonに、stateの情報が渡されている
- 17. const App = ()=> {
return <MainWindow>
<Frame1>
<Form1>
<Foo />
</Form1>
<Form2>
<Bar />
</Form2>
</Frame1>
</MainWindow>;
}
「状態」のバケツリレー
• 左のように階層が嵩むと、
stateを渡すための属性の
ため、定義がどんどん肥大
化しがち