- æ¦è¦
- 01. éå°ãªProp Drilling
- 02. éå°ãªimport
- 03. componentå ã«éå°ã«ãã¸ãã¹ãã¸ãã¯ãè¨è¿°ãã
- 04. éå°ãªre-render
- 05. ãã³ããã¢useEffect
- 06. ãã³ãã㢠&&
- 07. ãã³ããã¢ä¸é æ¼ç®å
- 08. propsããã®ã¾ã¾ä½¿ç¨ãã
- 09. React.VCãReact.VFCã使ç¨ãã
- 10. ç¡åé¢æ°ã使ç¨ããonXã®æ±æ
- 11. ã¤ã³ã©ã¤ã³ã¹ã¿ã¤ã«ã使ç¨ãã
- 12. import React from 'react'; ã使ç¨ãã
- ã¾ã¨ã
- åèè¨äº
æ¦è¦
ã¿ãªããããã«ã¡ã¯ããã«ã¹ã¿ãã¯ã¨ã³ã¸ãã¢ã®é«ç¬ @takasehiromichi ã§ãã
Reactã¯ãè¨è¿°ã®èªç±åº¦ãé«ãä¸æ¹ã§ãBad Practiceã¨ããããã®ããããããããªããæ¸ãæ¹ãåå¨ãã¾ãã
ã¨ã³ã¸ãã¢ã«ãã£ã¦ãBad Practiceãææ¡ãã¦ãã人ã¨ãããã§ãªã人ããããããBad Practiceã«ã¤ãã¦è¨äºã«ãããã¨æãã¾ãã
01. éå°ãªProp Drilling
Reactã¯propsãåcomponentã«æ¸¡ããã¨ãã§ãã¾ãããæè¡çã«ã¯ç¡éã«propsã渡ãç¶ãããã¨ãã§ãã¾ãã (親 > 親ã®å > 親ã®åã®å > 親ã®åã®åã®å ...)
ããããéå°ãªprop drillingã¯ãæ§ã ãªè¦çãä¼´ãã¾ãã
stateãæ´æ°ãããæãpropsãæ´æ°ãããæã親componentãre-renderãããæã«ãcomponentã¯re-renderããã¾ãã
ãã®ãããéå°ãªprop drillingã¯re-renderã®åé¡ãå¼ãèµ·ããå¯è½æ§ãããã¾ãã
ã¾ããéå°ãªprop drillingã¯å¯èªæ§ãä½ä¸ããã¾ãã
ãã®ãããpropsã渡ãæ°ã¯ãªãã¹ãå°ãªããªãããåªåããæ¹ãããã§ãããã
å人çã«ã¯ãuseContextã®ä½¿ç¨ããããããã¾ããã¾ããcomponentã¯ã親ãåãå«ã¾ã§ã¨ãããã以ä¸ã«ãªããããªæã¯ãã³ã¼ãã®æ§æãè¦ç´ãã¾ãã
02. éå°ãªimport
ä¾ãã°ãMaterial UIã®Buttonã使ç¨ãããå ´åã«ã
import * as MUI from '@material-ui/core'; <MUI.Button>aaa</MUI.Button>
ã¨ãæ¸ããã¨ãã§ãã¾ãã
ãããããã®è¨è¿°ã¯coreã®å ¨ã¦ããã¼ããããããé常ã«åé·ã§ãã
ãã®ãããæ¬å½ã«å¿ è¦ãªãã®ã ããã¼ãã§ããããã«è¨è¿°ããã®ãæã¾ããã§ãã
import { Button } from '@material-ui/core'; <Button>aaa</Button>
03. componentå ã«éå°ã«ãã¸ãã¹ãã¸ãã¯ãè¨è¿°ãã
componentå ã«ãã¸ãã¹ãã¸ãã¯ã大éã«è¨è¿°ããã¦ããã¨ãå¯èªæ§ãä½ä¸ãã¾ãã
componentã¨ãã¦ã®ç²åº¦ãé©åããuseReducerãuseContextã§ä»£æ¿å¯è½ãããã¸ãã¹ãã¸ãã¯ãutilã¨ãã¦å¤åºãã§ããªãããæ¤è¨ãã¹ãã§ãã
componentå ã®ãã¸ãã¹ãã¸ãã¯ã¯å¿ è¦æä½éã¨ããããã¾ã§UIãæç»ãããã¨ãã¡ã¤ã³ã¨ããæ¹ãããã§ãããã
04. éå°ãªre-render
componentã¨ãã¦åãåãã¦ããã¨ãéå°ãªre-renderãçºçãããã¨ãããããã¾ãã
useMemoãuseCallbackãé©åã«ä½¿ç¨ãã¦ãéå°ãªre-renderãæå¶ãã¾ãããã
05. ãã³ããã¢useEffect
componentãrenderãããã¨ãã«å¦çãããããã¨è¨ã£ã¦ããã³ããã¢useEffectã使ç¨ããã°ããã¨ãããã¨ã§ã¯ããã¾ããã
Reactãæ¨å¥¨ããuseEffectã®ä½¿ç¨æ¹æ³ã¯ä»¥ä¸ãªã³ã¯å ã«ããã¾ãã®ã§ãåèã«ããæ¹ãããã§ãããã
ããã¤ãæç²ãã¾ãã
- componentã表示ããã¦ããéããããã¯ã¼ã¯ãAPIãã©ã¤ãã©ãªã¨æ¥ç¶ããã¾ã¾ã«ãã¦ããããã®å¾ãæ¥ç¶ã解é¤ããå¿ è¦ããã
- setInterval()ã¨clearInterval()
- windows.addEventListener()ã¨window.removeEventListener()
- animation.start()ã¨animation.reset()
- ã«ã¹ã¿ã ããã¯ã§å©ç¨
- é React ã¦ã£ã¸ã§ããã®å¶å¾¡
- fetch
06. ãã³ãã㢠&&
&& ã¯ä¾¿å©ã§ãããåé¢ãæå³ããªãæåã«ãªãå¯è½æ§ãããã¾ãã
ä¾ãã°ã以ä¸ã®ã³ã¼ãã¯0ã表示ãã¾ãã
const amount = 0; return <span>{amount && `ç¾å¨ã®æ°é㯠${amount} ã§ãã`}</span>;
ã¾ããUIæç»é¨åã«ãã¸ãã¹ãã¸ãã¯ãæ··å ¥ãã¦ããããã好ã¾ããããã¾ããã
以ä¸ã®ããã«ãæ示çã«nullãè¿ããæ¹ãããã§ãããã
const Component = ({ amount }: { amount: number }): JSX.Element | null => { if (amount) { return <span>{`ç¾å¨ã®æ°é㯠${amount} ã§ãã`}</span>; } else return null; }; const amount = 0; return <Component amount={amount} />;
07. ãã³ããã¢ä¸é æ¼ç®å
ä¸é æ¼ç®åã¯ä¾¿å©ã§ããã使ãæã誤ãã¨ãå¯èªæ§ã®ä½ä¸ãæãã¾ãã
ä¾ãã°ã以ä¸ã¯UIæç»é¨åã«ä¸é æ¼ç®åã使ç¨ããä¾ã§ãã
ããã¯ã¾ã èªããããããã¾ãããããã®è¨è¿°ã®ä»æ¹ãè¤æ°åãè³ãæã§ä½¿ç¨ããã¦ãããã©ãã§ããããããã£ã¨èªã¿ã«ããã¨æãã¾ãã
ãã¸ãã¹ãã¸ãã¯ã¯ãé¢æ°ãcomponentã«åãåºãã¾ãããã
const Component = ({ isAdmin }: { isAdmin: boolean }): JSX.Element => { return ( <> <span>ãããå ±éçãªæç« ã ã¨ãã¦</span> <div> {isAdmin ? ( <> <div>ããã¯ã¨ã¦ãadminãªããã¹ãã§ãã</div> <div> <span>ä¾ãã°ãããªæãæ¥ã¦</span> <div> <span>ãããªæãæ¥ããã¨ããã¦</span> </div> </div> </> ) : ( <> <div>ããã¯adminã§ã¯ãªãã§ãã</div> <span>ä¾ãã°ãããªæãæ¥ã¦</span> <div> <span>ãããªæãæ¥ããã¨ããã¦</span> <span>ãããªæãæ¥ããã¨ããã¦</span> </div> </> )} </div> <span>ããã«footerãªãããæ¥ãããã¦</span> </> ); }; const isAdmin = true; return <Component isAdmin={isAdmin} />;
以ä¸ã¯ãé¢æ°ã«ã¾ã¨ãã¦ã¿ãä¾ã§ãã
const renderAdminText = (isAdmin: boolean): JSX.Element | undefined => { let adminTextJSX = null; if (isAdmin) { adminTextJSX = ( <> <div>ããã¯ã¨ã¦ãadminãªããã¹ãã§ãã</div> <div> <span>ä¾ãã°ãããªæãæ¥ã¦</span> <div> <span>ãããªæãæ¥ããã¨ããã¦</span> </div> </div> </> ); } else { adminTextJSX = ( <> <div>ããã¯adminã§ã¯ãªãã§ãã</div> <span>ä¾ãã°ãããªæãæ¥ã¦</span> <div> <span>ãããªæãæ¥ããã¨ããã¦</span> <span>ãããªæãæ¥ããã¨ããã¦</span> </div> </> ); } return <div>{adminTextJSX}</div>; }; const Component = ({ isAdmin }: { isAdmin: boolean }): JSX.Element => { return ( <> <span>ãããå ±éçãªæç« ã ã¨ãã¦</span> {renderAdminText(isAdmin)} <span>ããã«footerãªãããæ¥ãããã¦</span> </> ); }; const isAdmin = true; return <Component isAdmin={isAdmin} />;
08. propsããã®ã¾ã¾ä½¿ç¨ãã
propsãpropsããã¾ã¾ä½¿ç¨ããã¨ãpropsãã©ããªããããã£ãæã¤ãããããªããããå¯èªæ§ã®ä½ä¸ããããã¬ãã°ãåãå±éºæ§ãããã¾ãã
const Component = (props: any) => { return ( <div> <h1>{props.title}</h1> <span>{props.text}</span> </div> ); }; return <Component title="ã¿ã¤ãã«" text="ããã¹ã" />;
XPropsTypeã®åãå®ç¾©ã®ä¸ãåå²ä»£å ¥å¼æ°ã¨ãã¦ããããã£ãåãåãã¾ãããã
type ComponentPropsType = { title: string; text: string; }; const Component = ({ title, text }: ComponentPropsType) => { return ( <div> <h1>{title}</h1> <span>{text}</span> </div> ); }; return <Component title="ã¿ã¤ãã«" text="ããã¹ã" />;
09. React.VCãReact.VFCã使ç¨ãã
TypeScriptã®æèã«ããã¦ã¯ãReact.VCãReact.VFCã使ç¨ããã¡ãªããã¯ããã¾ããã®ã§ãJSX.Elementã使ç¨ãã¾ãã
â»å¾è¿°ã®åèè¨äºãåç §ãã¦ãã ããã
React.VCã®ä½¿ç¨
type ComponentPropsType = { title: string; text: string; }; const Component2: React.FC<ComponentPropsType> = ({ title, text, children, }) => { return ( <div> <span>{title}</span> <span>{text}</span> {children} </div> ); };
React.VFCã®ä½¿ç¨
type ComponentPropsTypeWithChildren = { title: string; text: string; children: JSX.Element; }; const Component3: React.VFC<ComponentPropsTypeWithChildren> = ({ title, text, children, }) => { return ( <div> <span>{title}</span> <span>{text}</span> {children} </div> ); };
JSX.Elementã®ä½¿ç¨
type ComponentPropsTypeWithChildren = { title: string; text: string; children: JSX.Element; }; const Component1 = ({ title, text, children, }: ComponentPropsTypeWithChildren): JSX.Element => { return ( <div> <span>{title}</span> <span>{text}</span> {children} </div> ); };
10. ç¡åé¢æ°ã使ç¨ããonXã®æ±æ
onXå ã§ç¡åé¢æ°ã使ç¨ããã¨UIã ãã§ãªããã¸ãã¹ãã¸ãã¯ãæ··å ¥ããå¯èªæ§ãä½ä¸ãã¾ãã
const Component = () => { type eType = React.MouseEvent<HTMLButtonElement, MouseEvent>; const doSomething1 = (e: eType) => { console.log('doSomething1', e); }; const doSomething2 = (e: eType) => { console.log('doSomething2', e); }; return ( <button onClick={(e) => { doSomething1(e); doSomething2(e); }} > test </button> ); };
onXã«ã¯handleXããã¦ããã®ä¸ã§ãã¸ãã¹ãã¸ãã¯ãè¨è¿°ãã¾ãã
const Component = () => { type eType = React.MouseEvent<HTMLButtonElement, MouseEvent>; const doSomething1 = (e: eType) => { console.log('doSomething1', e); }; const doSomething2 = (e: eType) => { console.log('doSomething2', e); }; const handleOnClick = (e: eType) => { doSomething1(e); doSomething2(e); }; return <button onClick={handleOnClick}>test</button>; };
11. ã¤ã³ã©ã¤ã³ã¹ã¿ã¤ã«ã使ç¨ãã
ã¤ã³ã©ã¤ã³ã¹ã¿ã¤ã«ã¯å¤§ããå¯èªæ§ãä½ä¸ããã¾ãã
åºæ¬çã«ã¯ãemotionã使ç¨ããæ¹ãããã§ãããçµç¹æåã«åããã¦é¸å®ããã®ãããã§ãããã
emotionãªããJSX Pragmaãä¸è¦ã§ã以ä¸ã ãã§åãã¾ãã
tsconfig.json
{ "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "@emotion/react" } }
â»å¾è¿°ã®åèè¨äºãåç §ãã¦ãã ããã
12. import React from 'react'; ã使ç¨ãã
React 17ããã¯ãimport React from 'react';
ã¯ä¸è¦ã§ãã
â»å¾è¿°ã®åèè¨äºãåç §ãã¦ãã ããã
ã¾ã¨ã
ã¾ã ã¾ã bad practiceã¯ããããã§ãããä¸æ¦ããã¾ã§ã§ã¾ã¨ãã¨ãããã¨æãã¾ãã