[react]道具と道具の種類とreactmemo
1.propsとeventListener
📌 componentのpropsとHTML要素のeventListerの違いを理解します!
function App() {
const [value, setValue] = React.useState("Save Changes");
const changeValue = () => setValue("Revert Changes");
return (
<div>
<Btn text={value} changeValue={changeValue} />
<Btn text="Continue" />
</div>
);
}
textやbooleanだけでなくchangeValueなどの関数も使用できますfunction Btn({ text, changeValue }) {
return (
<button
onClick={changeValue}
style={{
// 생략
}}
>
{text}
</button>
);
}
😎 覚えておきたい
propsに値を入力しても、戻りのHTML要素は自動的に入力されません.
2. props
🔨 インストールと設定
(1)取付
// PropTypes
<script src="https://unpkg.com/[email protected]/prop-types.js"></script>
// 첫번째 script source도 아래로 바꾸어줄 것 (버전)
<script src="https://unpkg.com/[email protected]/umd/react.development.js "></script>
(2)設定
// 리액트에 propTypes 설명하기
Btn.propTypes = {
text: PropTypes.string,
fontSize: PropTypes.number,
};
✅ prop types
📌 必要な支柱の識別を支援
Btn.propTypes = {
text: PropTypes.string.isRequired,
fontSize: PropTypes.number.isRequired,
};
componentはtextとfontSizeの2つの道具しか提供していません.💖 メリット
function App() {
return (
<div>
<Btn text="Save Changes" fontSize={18} />
<Btn text={14} fontSize={"Continue"} />
</div>
);
}
proptypeのインストール
proptypeのインストール
以下に示すように、コンソールでエラーを確認できます.
3. .isRequired
📌 構成部品のpropsが変わらない場合は、提供された関数の再レンダリングを防止できます.
React Memo
を使用します.function App() {
const [value, setValue] = React.useState("Save Changes");
const changeValue = () => setValue("Revert Changes");
return (
<div>
<Btn text={value} onClick={changeValue} />
<Btn text="Continue" />
</div>
);
}
🔎 いつ使いますか。
React.memo
レンダリングコンポーネントconst MemorizedBtn = React.memo(Btn);
function App() {
const [value, setValue] = React.useState("Save Changes");
const changeValue = () => setValue("Revert Changes");
return (
<div>
<MemorizedBtn text={value} onClick={changeValue} />
<MemorizedBtn text="Continue" />
</div>
);
}
Reference
この問題について([react]道具と道具の種類とreactmemo), 我々は、より多くの情報をここで見つけました https://velog.io/@hye_rin/React-props와-prop-types-그리고-React.memoテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol