React v18.0
March 29, 2022 by The React Team
React 18 ã npm ã§å©çšå¯èœã«ãªããŸããïŒ ååã®æçš¿ã«ãŠãã¢ããªã React 18 ã«ã¢ããã°ã¬ãŒãããããã®ã¹ããããã€ã¹ãããã¬ã€ããå ±æããŸããããã®æçš¿ã§ã¯ãReact 18 ã®æ°æ©èœããå°æ¥ã«åããŠã®å±æããäŒãããŸãã
ãã®ææ°ã®ã¡ãžã£ãŒããŒãžã§ã³ã«ã¯ãèªåãããã³ã° (automatic batching) ã®ãããªèªåã§æå¹ã«ãªãæ©èœæ¹åãstartTransition ã®ãããªæ°ã㪠APIããããŠãµã¹ãã³ã¹ (suspense) ã«å¯Ÿå¿ããã¹ããªãŒãã³ã°ã§ã®ãµãŒããµã€ãã¬ã³ããªã³ã°æ©èœãå«ãŸããŠããŸãã
React 18 ã®æ©èœã®å€ããåºç€ãšããŠããã®ã¯æ°ãã«å ãã£ã䞊è¡ã¬ã³ãã© (concurrent renderer) ã§ãããããã匷åãªæ°æ©èœçŸ€ãå®çŸããããã«è£ã§åãããã«ãªã£ãŠããŸããReact ã®äžŠè¡åŠçæ©èœã¯ãªããã€ã³ã§ããã䞊è¡åŠçæ©èœã䜿ãå Žåã«ã®ã¿æå¹ã«ãªããã®ã§ãããããã¯çããã®ã¢ããªäœææ¹æ³ã«å€§ããªåœ±é¿ãäžãããã®ã§ãããšæã£ãŠããŸãã
æã 㯠React ã§äžŠè¡åŠçããµããŒãããããã«äœå¹Žãã®ããã ç 究éçºãéããŠããŠãããç¹ã«æ¢åãŠãŒã¶ã段éçã«æ¡çšã§ããæ¹æ³ãæäŸããããšã«é¢ããŠã¯æ³šæãæã£ãŠããŸãããæšå¹Žã®å€ã« React 18 ã¯ãŒãã³ã°ã°ã«ãŒããäœæãããšãã¹ããŒããã³ãã¥ããã£ãããã£ãŒãããã¯ãéããReact ã®ãšã³ã·ã¹ãã å šäœãã¹ã ãŒã¹ã«ã¢ããã°ã¬ãŒãã§ããããã«ããŠããŸããã
ãŸããReact Conf 2021 ã§ãå€ãã®ããšãå ±æããŠããŸããã
- ããŒããŒãã§ã¯ãçŽ æŽããããŠãŒã¶äœéšãéçºè ãç°¡åã«æ§ç¯ã§ããããã«ãããšããæã ã®äœ¿åœã« React 18 ãã©ãé¢ããã®ãã«ã€ããŠèª¬æããŠããŸãã
- Shruti Kapoor 㯠React 18 ã®æ°æ©èœã®äœ¿ãæ¹ã«ã€ããŠãã¢ãè¡ã£ãŠããŸãã
- Shaundai Person ã¯ãµã¹ãã³ã¹ãçšããã¹ããªãŒãã³ã°ãµãŒãã¬ã³ããªã³ã°ã«ã€ããŠã®æŠèŠã説æããŠããŸãã
以äžãã䞊è¡ã¬ã³ããŒæ©èœãã¯ãããšããããã®ãªãªãŒã¹ã§æåŸ ãããæ°èŠçŽ ã®æŠèŠã§ãã
React ã®äžŠè¡åŠçæ©èœãšã¯ïŒ
React 18 ã§å ãã£ãäžã§æãéèŠãªãã®ãšã¯ããªãã¡ã䞊è¡åŠçæ©èœãã§ãããããã¯ã§ããããšãªãçãããå šãèããªãã§æžãã®ãæãŸããããšã§ãããã ãã¢ããªã±ãŒã·ã§ã³éçºè ã«ãšã£ãŠã¯æŠããã®éãã§ãããããã©ã€ãã©ãªã®ã¡ã³ããã«ãšã£ãŠã¯è©±ã¯ã¡ãã£ãšè€éãããããŸããã
䞊è¡åŠçã¯ãããèªäœãäœãæ©èœã ãšããããã§ã¯ãããŸãããããã¯ãåæã« UI ã®è€æ°ã®ããŒãžã§ã³ã React ãæºåããŠãããããã«ããããã®ãæ°ããªè£æ¹ã®ã¡ã«ããºã ã§ãã䞊è¡åŠçæ©èœãã®ãã®ã¯å®è£ ã®è©³çŽ° (implementation detail) ã«éããããããæçšãªã®ã¯ããã§å®çŸã§ããæ§ã ãªæ©èœãååšããããããã ããšèããŠãã ãããReact ã¯å éšã®å®è£ ã«ãã㊠priority queue ã multiple buffering ãªã©ã®æŽç·Žãããææ³ãçšããŠããŸããã§ãããããã®ã³ã³ã»ããã¯å ¬é API ã®ã©ãã«ãçŸããŸããã
æã ã API ãèšèšããéã«ã¯ãéçºè ã«å®è£ ã®è©³çŽ°ãèŠããªãããã«åªåããŠããŸããReact éçºè ãšããŠã¿ãªããã¯ãŠãŒã¶äœéšãã©ããªèŠãç®ã«ãããããèããããšã«éäžãããã®èŠãç®ãã©ã®ããã«å®çŸããã®ã㯠React ãåãæã¡ãŸããReact éçºè ããè£ã§äžŠè¡åŠçæ©èœãã©ã®ããã«åããŠããã®ããç¥ãå¿ èŠã¯ãããŸããã
ããããReact ã®äžŠè¡åŠçæ©èœã¯ãæ®éã®ãå®è£ ã®è©³çŽ°ããšæ¯ã¹ãŠããéèŠãªãã®ã§ãããReact ã®ã³ã¢ã®ã¬ã³ããªã³ã°ã¢ãã«ã«å¯Ÿããæ¬è³ªçãªå€æŽã§ããã§ãã®ã§äžŠè¡åŠçã®åäœã«ã€ããŠè©³ããç¥ãããšããã®åãéèŠãšããããšã§ã¯ãªãã«ãããã©ã®ãããªãã®ãã«ã€ããŠé«ã¬ãã«ã®æŠèŠ³ãç¥ã£ãŠããããšã¯æçšãããããŸããã
React ã®äžŠè¡åŠçæ©èœã®éèŠãªç¹æ§ã¯ãåŠçãäžæå¯èœã§ãããšããããšã§ããReact 18 ã«ã¢ããã°ã¬ãŒãããŠããäœããã®äžŠè¡åŠçæ©èœãå ãããŸã§ã¯ãæŽæ°ã¯ React ã®ä»¥åã®ããŒãžã§ã³ãšåããããŸãšããŠãäžæãããããã€åæçã«ã¬ã³ããŒãããŸããåæçãªã¬ã³ããŒã§ã¯ãæŽæ°ã®ã¬ã³ããŒãå§ãŸã£ããããŠãŒã¶ãçµæãç»é¢ã§èŠãããããã«ãªããŸã§ãããäžæããããšã¯ã§ããŸããã
䞊è¡ã¬ã³ããŒã«ãããŠã¯ããããå¿ ãããæ£ãããªããªããŸããReact ã¯æŽæ°ã®ã¬ã³ããŒãéå§ããéäžã§äžæåæ¢ããåŸã§åéããããšãã§ããŸããéäžãŸã§çµãã£ãã¬ã³ããŒãå®å šã«æšãŠãŠããŸãããšãããããŸããã¬ã³ããŒãäžæãããšããŠããReact 㯠UI ã®èŠãç®ã«äžè²«æ§ãããããšãä¿èšŒããŸãããããå®çŸããããã«ãReact ã¯ããªãŒå šäœã®è©äŸ¡ãçµãããŸã§ DOM ã®æžãæããããã«åŸ æ©ããŸããããã«ãããReact ã¯ã¡ã€ã³ã¹ã¬ããããããã¯ããã«ããã¯ã°ã©ãŠã³ãã§æ¬¡ã®ç»é¢ãçšæããŠãããããã«ãªããŸããã€ãŸãã倧ããªã¬ã³ããŒäœæ¥ã®æäžã§ããŠãŒã¶ã®å ¥åã« UI ãå³åº§ã«åå¿ã§ãããšããããšã§ããããŠãŒã¶äœéšãã¹ã ãŒã¹ã«ãªããŸãã
ãã 1 ã€ã®äŸã¯ãstate ã®åå©çšã§ããReact ã®äžŠè¡åŠçæ©èœã«ãããç»é¢ãã UI ã®äžéšåããã£ããåé€ããååã® state ãåå©çšããªããåŸã§æ»ãããšããããšãå¯èœã§ããäŸãã°ããŠãŒã¶ãã¿ããåãæ¿ããŠç»é¢ããé¢ããŠæ»ã£ãŠããå ŽåãReact ã¯ä»¥åã®ç»é¢ã以åãšåæ§ã® state ã§åŸ©åž°ãããå¿
èŠããããŸããå°æ¥ã®ãã€ããŒãªãªãŒã¹ã«ãããŠããã®ãã¿ãŒã³ãå®è£
ãã <Offscreen>
ãšããã³ã³ããŒãã³ããæ°ãã«å ããäºå®ã§ããåæ§ã«ã<Offscreen>
ã䜿ã£ãŠããã¯ã°ã©ãŠã³ãã§æ°ãã UI ãçšæãããŠãŒã¶ã衚瀺ãããããšããåã«æºåå®äºã«ããŠããããšããããšãã§ããããã«ãªãã§ãããã
䞊è¡ã¬ã³ããŒã¯ React ã«ãããæ°ãããã¯ãã«ãªããŒã«ã§ããããµã¹ãã³ã¹ããã©ã³ãžã·ã§ã³ãã¹ããªãŒãã³ã°ä»ããµãŒããŒã¬ã³ããªã³ã°ãšãã£ãæ°ããªæ©èœã®ã»ãšãã©ã¯ããã掻çšããŠæ§ç¯ãããŠããŸããããã React 18 ã¯ãã®æ°ããåºç€ã®äžã«æã ãæ§ç¯ããããšããŠãããã®ã®å§ãŸãã«éããŸããã
䞊è¡åŠçæ©èœã®æ®µéçãªæ¡çš
å³å¯ã«ã¯ã䞊è¡ã¬ã³ããŒã¯ç Žå£çå€æŽã§ãã䞊è¡ã¬ã³ããŒã¯äžæå¯èœãªããããããæå¹ã«ãªããšã³ã³ããŒãã³ãã¯ãããã«ç°ãªã£ãåäœãããŸãã
æã ã¯ãã¹ãã«ãããŠãæ°åã®ã³ã³ããŒãã³ãã React 18 ã®ããã«ã¢ããã°ã¬ãŒãããŸãããããã§åãã£ãããšã¯ãã»ãšãã©ãã¹ãŠã®æ¢åã®ã³ã³ããŒãã³ãã¯äžŠè¡ã¬ã³ããŒã«ãããŠããæ®éã«ãåäœãããšããããšã§ãããããããã€ãã®ã³ã³ããŒãã³ãã§ã¯ç§»è¡ã®ããã®è¿œå äœæ¥ãå¿ èŠã§ããå€æŽã¯éåžžå°ããªãã®ã§ãããèªåã®ããŒã¹ã§æŽæ°äœæ¥ãè¡ãããšãå¯èœã§ããReact 18 ã®æ°ããªã¬ã³ããŒã®æåã¯ãããªãã®ã¢ããªå ã§æ°æ©èœã䜿ã£ãŠããéšåã§ã®ã¿æå¹åãããŸãã
倧ãŸããªç§»è¡äœæ¥ã®æµããšããŠã¯ããŸãæ¢åã³ãŒãã®æåãå£ããã«ã¢ããªã React 18 ã§åäœããããã«ããŸããããããèªåã®ããŒã¹ã§äžŠè¡åŠçæ©èœãåŸã
ã«è¿œå ãå§ããããšãã§ããŸãã<StrictMode>
ãå©çšããã°ã䞊è¡åŠçã«é¢é£ãããã°ã«éçºæã«æ°ä»ããããããã«ã§ããŸããstrict ã¢ãŒãã¯æ¬çªã§ã®åäœã«åœ±é¿ãäžããŸããããéçºäžã«ã¯è¿œå ã®èŠåã衚瀺ããããã¹ãç (idempotent) ã§ããã¹ãé¢æ°ã 2 ååŒã³åºãããããŸãããã¹ãŠã®ééããææããããšã¯ã§ããŸããããæãããããééããé²ãã®ã«å¹æçã§ãã
React 18 ã«ã¢ããã°ã¬ãŒãåŸã䞊è¡åŠçæ©èœãããã«äœ¿ãå§ããããšãã§ããŸããäŸãã°ãŠãŒã¶ã®å ¥åããããã¯ããã«ç»é¢é·ç§»ãè¡ãããã« startTransition ã䜿ãããšãã§ããŸãããããã¯é«äŸ¡ãªåã¬ã³ããŒã®é »åºŠãèœãšãããã« useDeferredValue ã䜿ãããšãå¯èœã§ãã
ãããé·æçã«ã¯ãããªãã®ã¢ããªã«äžŠè¡åŠçãå ããããã®ã¡ã€ã³ã®æ¹æ³ã¯ã䞊è¡åŠçã«å¯Ÿå¿ããã©ã€ãã©ãªããã¬ãŒã ã¯ãŒã¯ã䜿ãããšã«ãªãã ãããšèããŠããŸããã»ãšãã©ã®å Žåãããªãã䞊è¡åŠçã® API ãçŽæ¥è§Šãããšã¯ãªãã¯ãã§ããäŸãã°æ°ããç»é¢ã«é·ç§»ãããã³ã«æ¯åéçºè ã startTransition ãã³ãŒã«ããã®ã§ã¯ãªããã«ãŒã¿ã®ã©ã€ãã©ãªãããã²ãŒã·ã§ã³ã startTransition ã§èªåã§ã©ããããããã«ãªãã§ãããã
ã©ã€ãã©ãªãã¢ããã°ã¬ãŒããããŠäžŠè¡åŠçæ©èœå¯Ÿå¿ã«ãªããŸã§ã«ã¯ãå€å°æéãããããããããŸããã䞊è¡åŠçæ©èœãã©ã€ãã©ãªã掻çšããããããããã«æ°ãã API ãæäŸããŠããŸããåœé¢ã¯ãReact ãšã³ã·ã¹ãã ãåŸã ã«ç§»è¡ããŠãããŸã§ãã©ã€ãã©ãªã¡ã³ãããäœæ¥ããã®ããåŸ ã¡ãã ããã
詳现ã¯ãååã®æçš¿ãã芧ãã ããïŒReact 18 ã¢ããã°ã¬ãŒãã¬ã€ã.
ããŒã¿ãã¬ãŒã ã¯ãŒã¯ã«ããããµã¹ãã³ã¹
React 18 ã§ã¯ãRelayãNext.jsãHydrogenãRemix ã®ãããªã䜿ãæ¹ã®èŠçŽããã (opinionated) ãã¬ãŒã ã¯ãŒã¯ã«ãããŠãããŒã¿ååŸã®ããã®ãµã¹ãã³ã¹ã䜿ãå§ããããšãã§ããŸããåçºçãªããŒã¿ååŸã«ãµã¹ãã³ã¹ã䜿ãããšãæè¡çã«ã¯å¯èœã§ãããçŸæç¹ã§ã¯äžè¬çãªæŠç¥ãšããŠã¯ãå§ãããŸããã
å°æ¥çã«ã¯ãäžèšã®ãããªãã¬ãŒã ã¯ãŒã¯ãçšããªããŠãããªãã®ããŒã¿ã«ãµã¹ãã³ã¹ã䜿ã£ãŠã¢ã¯ã»ã¹ãããããããããæ°ãã«åºæ¬æ©èœãæäŸãããããããŸãããããããµã¹ãã³ã¹ã¯ãã«ãŒã¿ãããŒã¿ã¬ã€ã€ããµãŒãã¬ã³ããªã³ã°ç°å¢ãšãã£ãããªãã®ã¢ããªã®ã¢ãŒããã¯ãã£ãšæ·±ãçµåããŠå©çšãããå Žåã«ãæãå¹æãçºæ®ããŸããé·æçã«ããã©ã€ãã©ãªããã¬ãŒã ã¯ãŒã¯ã React ã®ãšã³ã·ã¹ãã ã«ãããŠéèŠãªåããããããšãæåŸ ããŠããŸãã
React ã®ä»¥åã®ããŒãžã§ã³ãšåæ§ããµã¹ãã³ã¹ã¯ã¯ã©ã€ã¢ã³ã㧠React.lazy ã䜿ã£ãŠã³ãŒããåå²ããéã«ãå©çšã§ããŸãããããæã ããµã¹ãã³ã¹ã䜿ã£ãŠå®çŸããããšæ§æ³ããŠããã®ã¯ãã³ãŒãã®ããŒãããããã£ãšå€ãã®ããšã§ããç®æšã¯ããµã¹ãã³ã¹ã®ãµããŒããæ¡åŒµããŠããããããã¯ãµã¹ãã³ã¹ã«ããã²ãšã€ã®å®£èšçãªãã©ãŒã«ããã¯ããããããéåæçãªæäœïŒã³ãŒããããŒã¿ãç»åãªã©ã®ããŒãïŒãæ±ããããã«ããããšã§ãã
ãµãŒãã³ã³ããŒãã³ãã¯ãŸã éçºäžã§ã
ãµãŒãã³ã³ããŒãã³ã ã¯å®è£ äºå®ã®æ©èœã§ãããã¯ã©ã€ã¢ã³ããµã€ãã¢ããªã«ããããªãããªã€ã³ã¿ã©ã¯ãã£ããã£ãšäŒçµ±çãªãµãŒãã¬ã³ããªã³ã°ã«ããããã©ãŒãã³ã¹æ¹åãšãå Œãåãããã¯ã©ã€ã¢ã³ãã»ãµãŒãäž¡æ¹ã«ãŸãããã¢ããªã®éçºãå¯èœã«ãããã®ã§ãããµãŒãã³ã³ããŒãã³ã㯠React ã®äžŠè¡åŠçæ©èœãšæ¬è³ªçã«çµåããŠã¯ããŸãããããµã¹ãã³ã¹ãã¹ããªãŒãã³ã°ãµãŒãã¬ã³ããªã³ã°ã®ãããªäžŠè¡åŠçæ©èœãšäœµçšããéã«æãããŸãåããããã¶ã€ã³ãããŠããŸãã
ãµãŒãã³ã³ããŒãã³ãã¯ãŸã å®éšçæ©èœã§ããã18.x ã®ãã€ããŒãªãªãŒã¹ã§åæããŒãžã§ã³ããªãªãŒã¹ã§ããèŠèŸŒã¿ã§ãããããŸã§ã¯ãããããŒã¶ã«ãæšãé²ããŠåºãæ¡çšã§ããæºåãæŽããããNext.jsãHydrogenãRemix ã®ãããªãã¬ãŒã ã¯ãŒã¯ãšååããŠãããŸãã
React 18 ã®æ°èŠçŽ
æ°æ©èœïŒèªåãããã³ã°
ãããã³ã°ãšã¯ React ãããã©ãŒãã³ã¹ã®ããã«è€æ°ã®ã¹ããŒãæŽæ°ãã°ã«ãŒãåããŠãåäžã®åã¬ã³ããŒã«ãŸãšããããšãæããŸããèªåãããã³ã°ä»¥åã¯ãReact ã®ã€ãã³ããã³ãã©å ã§ã®æŽæ°ã®ã¿ãããåŠçãããŠããŸãããpromise ã setTimeoutããã€ãã£ãã®ã€ãã³ããã³ãã©ããã®ä»ããããã€ãã³ãå ã§èµ·ããæŽæ°ã¯ããã©ã«ãã§ã¯ãããåŠçãããŠããŸããã§ãããèªåãããã³ã°ã«ããããããã®æŽæ°ãèªåã§ãããåŠçãããããã«ãªããŸãïŒ
// Before: only React events were batched.
setTimeout(() => {
setCount(c => c + 1);
setFlag(f => !f);
// React will render twice, once for each state update (no batching)
}, 1000);
// After: updates inside of timeouts, promises,
// native event handlers or any other event are batched.
setTimeout(() => {
setCount(c => c + 1);
setFlag(f => !f);
// React will only re-render once at the end (that's batching!)
}, 1000);
詳现ã«ã€ããŠã¯ãAutomatic batching for fewer renders in React 18 ãåç §ããŠãã ããã
æ°æ©èœïŒãã©ã³ãžã·ã§ã³
ãã©ã³ãžã·ã§ã³ïŒtransition; 段éçæšç§»ïŒãšã¯ React ã«ãããæ°ããªæŠå¿µã§ãããç·æ¥æ§ã®é«ãæŽæ° (urgent update) ãšé«ããªãæŽæ° (non-urgent update) ãåºå¥ããããã®ãã®ã§ãã
- ç·æ¥æ§ã®é«ãæŽæ°ãšã¯ã¿ã€ããã¯ãªãã¯ããã¬ã¹ãšãã£ããŠãŒã¶æäœãçŽæ¥åæ ãããã®ã§ãã
- ãã©ã³ãžã·ã§ã³ã«ããæŽæ°ã¯ UI ãããç»é¢ããå¥ã®ç»é¢ã«æ®µéçã«é·ç§»ããããã®ã§ãã
ã¿ã€ããã¯ãªãã¯ããã¬ã¹ã®ãããªç·æ¥æ§ã®é«ãæŽæ°ã¯ãç©ççãªç©äœã®æåã«é¢ããæã ã®çŽèŠ³ã«åããªããããå³åº§ã«åå¿ããå¿ èŠããããããã§ãªããšãããããããšèªèãããŠããŸããŸããäžæ¹ã§ãã©ã³ãžã·ã§ã³å ã§ã¯ããŠãŒã¶ã¯ç»é¢äžã§ããããäžéã®å€ãèŠããããšãæåŸ ããŠããŸããã
äŸãã°ãããããããŠã³å ã§ãã£ã«ã¿ãéžæããå Žåããã£ã«ã¿ãã¿ã³èªäœã¯ã¯ãªãã¯ããç¬éã«åå¿ããããšãæåŸ ããã§ããããããããã£ã«ã¿ã®çµæã¯ããã¿ã³ã®åå¿ãšã¯å¥ã«åŸã ã«çŸããŠãæ§ããŸãããå°ããªé 延ã¯èªèã§ããŸãããããã£ãŠæ§ããªããã®ã§ãããŸããåã®ã¬ã³ããŒãçµãã£ãŠããªã段éã§åã³ãã£ã«ã¿ãå€æŽããå Žåãæçµçãªçµæ以å€ã¯æ°ã«ããŸããã
å žåçã«ã¯ãæè¯ã®ãŠãŒã¶äœéšã®ããã«ã¯ãããã²ãšã€ã®ãŠãŒã¶å ¥åã¯ç·æ¥æ§ã®é«ãæŽæ°ãšé«ããªãæŽæ°ã®äž¡æ¹ãåŒãèµ·ããããã«ããã¹ãã§ããinput ã€ãã³ãå 㧠startTransition API ãçšããReact ã«ã©ã®æŽæ°ã®ç·æ¥æ§ãé«ããã©ããããã©ã³ãžã·ã§ã³ããªã®ããäŒããããšãã§ããŸãïŒ
import { startTransition } from 'react';
// Urgent: Show what was typed
setInputValue(input);
// Mark any state updates inside as transitions
startTransition(() => {
// Transition: Show the results
setSearchQuery(input);
});
startTransition ã§ã©ããããæŽæ°ã¯ç·æ¥æ§ã®äœããã®ãšããŠæ±ãããã¯ãªãã¯ãããŒæŒäžã®ãããªç·æ¥æ§ã®é«ãæŽæ°ããã£ãŠããå Žåã«ã¯äžæãããŸãããã©ã³ãžã·ã§ã³ããŠãŒã¶ã«ãã£ãŠäžæãããå ŽåïŒäŸãã°çŽ æ©ãè€æ°ã®ã¿ã€ããèµ·ãã£ãå ŽåïŒãReact ã¯å®äºããªããŸãŸã«å€ããªã£ãã¬ã³ããŒãç Žæ£ããŠãæåŸã®æŽæ°ã®ã¿ã¬ã³ããŒããŸãã
useTransition
: ãã©ã³ãžã·ã§ã³ãéå§ããããã®ããã¯ã§ãããä¿çäžãã©ããã®ç¶æ ã远跡ããããã®å€ãå«ãŸããŸããstartTransition
: ããã¯ã䜿ããªãå Žåã«ãã©ã³ãžã·ã§ã³ãéå§ããããã®ã¡ãœããã§ãã
ãã©ã³ãžã·ã§ã³ã䜿ããšäžŠè¡ã¬ã³ããŒæ©èœã«ãªããã€ã³ããæŽæ°ãäžæå¯èœã«ãªããŸãããŸããã³ã³ãã³ããåãµã¹ãã³ãããå Žåãããã¯ã°ã©ãŠã³ãã§ãã©ã³ãžã·ã§ã³äžã®ã³ã³ãã³ããã¬ã³ããŒãã€ã€ãçŸåšã®ã³ã³ãã³ãã衚瀺ãç¶ãããã React ã«äŒããŸãïŒè©³çŽ°ã«ã€ããŠã¯ ãµã¹ãã³ã¹ RFC ãåç §ïŒã
ãã©ã³ãžã·ã§ã³ã®ããã¥ã¡ã³ãã¯ãã¡ãã
ãµã¹ãã³ã¹ã®æ°æ©èœ
ãµã¹ãã³ã¹ã«ãããã³ã³ããŒãã³ãããªãŒã®äžéšããŸã 衚瀺ã§ããªãå Žåã«ãããŒãäžãšããç¶æ ã宣èšçã«èšè¿°ã§ããããã«ãªããŸãïŒ
<Suspense fallback={<Spinner />}>
<Comments />
</Suspense>
ãµã¹ãã³ã¹ã«ããããUI ããŒãäžç¶æ ããšãããã®ããReact ã®ããã°ã©ãã³ã°ã¢ãã«ã§å®£èšçã«èšè¿°å¯èœãªäž»èŠã³ã³ã»ããã«ææ ŒããŸãããã®äžã«ããé«ã¬ãã«ãªæ©èœãæ§ç¯ããŠãããããã«ãªããŸãã
æã ã¯æ°å¹Žåã«æ©èœéå®çã®ãµã¹ãã³ã¹ãå°å ¥ããŸãããããããµããŒããããŠãããŠãŒã¹ã±ãŒã¹ã¯ React.lazy ã«ããã³ãŒãåå²ã®ã¿ã§ããããµãŒãã§ã®ã¬ã³ããŒã«ãããŠã¯äžåãµããŒããããŠããŸããã§ããã
React 18 ã§ã¯ãµãŒãåŽã§ã®ãµã¹ãã³ã¹ã®ãµããŒããè¿œå ãã䞊è¡ã¬ã³ããªã³ã°æ©èœãçšããŠãã®èœåãåäžãããŸããã
React 18 ã«ããããµã¹ãã³ã¹ã¯ãã©ã³ãžã·ã§ã³ API ãšäœµçšããå Žåã«èœåãçºæ®ããŸãããã©ã³ãžã·ã§ã³äžã§ãµã¹ãã³ããçºçãããšãReact ã¯æ¢ã«èŠããŠããã³ã³ãã³ãããã©ãŒã«ããã¯ã«ãã£ãŠé ãããŠããŸããªãããã«ããã®ã§ãã代ããã«ãååãªããŒã¿ãããŒãããããŸã§ã¬ã³ããŒãé ãããŠãæãŸãããªãããŒãäžç¶æ ãèŠããªãããã«ããŸãã
詳ããã¯ãSuspense in React 18 ã® RFC ãåç §ããŠãã ããã
æ°ããªã¯ã©ã€ã¢ã³ãããã³ãµãŒãçšã®ã¬ã³ã㌠API
ãã®ãªãªãŒã¹ãæ©ã«ãã¯ã©ã€ã¢ã³ãããã³ãµãŒãçšã«å ¬éããŠãã API ãåèšèšããããšã«ããŸãããããã«ãã React 18 ã®æ°ãã API ã«ã¢ããã°ã¬ãŒããããŸã§ã®éãReact 17 ã®å€ã API ãå©çšãç¶ããããšãã§ããããã«ãªããŸãã
React DOM Client
以äžã®æ°ã㪠API 㯠react-dom/client
ãããšã¯ã¹ããŒããããããã«ãªã£ãŠããŸãïŒ
createRoot
:render
ãããunmount
ãããã§ããæ°ããªã«ãŒããäœæããããã®æ°ã¡ãœããã§ããReactDOM.render
ã®ä»£ããã«å©çšããŠãã ãããããã䜿ããªããš React 18 ã®æ°æ©èœã¯åäœããŸãããhydrateRoot
: ãµãŒãã§ã¬ã³ããŒãããã¢ããªããã€ãã¬ãŒã·ã§ã³ããããã®æ°ã¡ãœããã§ããReactDOM.hydrate
ã®ä»£ããã«ãæ°ã㪠React DOM ãµãŒã API ãšäœµããŠå©çšããŠãã ãããããã䜿ããªããš React 18 ã®æ°æ©èœã¯åäœããŸããã
createRoot
ãš hydrateRoot
ã®ãããããonRecoverableError
ãšããæ°ããªãªãã·ã§ã³ãåãåãããã«ãªã£ãŠãããã¬ã³ããŒãããã¯ãã€ãã¬ãŒã·ã§ã³äžã«èµ·ãããšã©ãŒãã React ã埩垰ããå Žåã«éç¥ãåããŠãã°ãæ®ãããå Žåã«å©çšã§ããŸããããã©ã«ãã§ã¯ React 㯠reportError
ããå€ããã©ãŠã¶ã®å Žå㯠console.error
ãå©çšããŸãã
React DOM Client ã®ããã¥ã¡ã³ãã¯ãã¡ãã
React DOM Server
以äžã®æ°ã㪠API 㯠react-dom/client
ãããšã¯ã¹ããŒããããããã«ãªã£ãŠããããµãŒãã§ãµã¹ãã³ã¹ãã¹ããªãŒãã³ã°ããæ©èœãå®å
šã«ãµããŒãããŠããŸãïŒ
renderToPipeableStream
: Node ç°å¢ã§ã®ã¹ããªãŒãã³ã°çšãrenderToReadableStream
: Deno ã Cloudflare Workers ã®ãããªã¢ãã³ãªãšããžã©ã³ã¿ã€ã ç°å¢çšã
æ¢åã® renderToString
ã¡ãœããã¯ä»åŸãåäœããŸãããæšå¥šãããŸããã
React DOM Server ã®ããã¥ã¡ã³ãã¯ãã¡ãã
Strict ã¢ãŒãã®æ°ããªæå
å°æ¥çã«ãReact ã state ãä¿ã£ããŸãŸã§ UI ã®äžéšåãè¿œå ã»åé€ã§ãããããªæ©èœãå°å ¥ããããšèããŠããŸããäŸãã°ããŠãŒã¶ãã¿ããåãæ¿ããŠç»é¢ãé¢ããŠããæ»ã£ãŠããå Žåã«ãReact ã以åã®ç»é¢ãããã«è¡šç€ºã§ããããã«ãããã®ã§ãããããå¯èœã«ãããããReact ã¯åã state ã䜿çšããŠããªãŒãã¢ã³ããŠã³ãã»åããŠã³ãããŸãã
ãã®æ©èœã«ãããReact ã®æšæºç¶æ ã§ã®ããã©ãŒãã³ã¹ãåäžããŸãããã³ã³ããŒãã³ãã¯å¯äœçšãäœåºŠãç»é²ããããç Žæ£ããããããããšã«å¯ŸããŠèæ§ãæã€ããšãå¿ èŠã«ãªããŸããã»ãšãã©ã®å¯äœçšã¯äœã®å€æŽããªãåäœããŸãããäžéšã®å¯äœçšã¯äžåºŠããç»é²ã»ç Žæ£ãããªããã®ãšæ³å®ããŠããŸãã
ãã®åé¡ã«æ°ä»ããããããããã«ãReact 18 㯠strict ã¢ãŒãã«æ°ããéçºæå°çšã®ãã§ãã¯ãå°å ¥ããŸãããã®æ°ãããã§ãã¯ã¯ãã³ã³ããŒãã³ããåããŠããŠã³ãããããã³ã«ããã¹ãŠã®ã³ã³ããŒãã³ããèªåçã«ã¢ã³ããŠã³ãã»åããŠã³ãããã〠2 åç®ã®ããŠã³ãã§ä»¥åã® state ã埩å ããŸãã
ãããŸã§ã¯ãReact ã¯ã³ã³ããŒãã³ããããŠã³ãããŠä»¥äžã®ããã«å¯äœçšãäœæããŠããŸããïŒ
* React ãã³ã³ããŒãã³ããããŠã³ã
* ã¬ã€ã¢ãŠãå¯äœçš (layout effect) ãäœæ
* ïŒéåžžã®ïŒå¯äœçšãäœæ
React 18 ã® strict ã¢ãŒãã§ã¯ãéçºæã«ã³ã³ããŒãã³ããããŠã³ããããå ŽåãReact ã¯ã³ã³ããŒãã³ãã®å³æã¢ã³ããŠã³ãã»åããŠã³ããã·ãã¥ã¬ãŒã·ã§ã³ããŸãïŒ
* React ãã³ã³ããŒãã³ããããŠã³ã
* ã¬ã€ã¢ãŠãå¯äœçšãäœæ
* å¯äœçšãäœæ
* ããŠã³ããããã³ã³ããŒãã³ãå
ã§å¯äœçšã®ç Žæ£ãã·ãã¥ã¬ãŒã
* ã¬ã€ã¢ãŠãå¯äœçšãç Žæ£
* å¯äœçšãç Žæ£
* ããŠã³ããããã³ã³ããŒãã³ãå
ã§ä»¥åã® state ã埩å
ãå¯äœçšã®åçæãã·ãã¥ã¬ãŒã
* ã¬ã€ã¢ãŠãå¯äœçšãäœæ
* å¯äœçšã®äœæçšã³ãŒãã®å®è¡
state åå©çšå¯èœæ§ã®ä¿èšŒã«ã€ããŠã®ããã¥ã¡ã³ãã¯ãã¡ãã
æ°ããªããã¯
useId
useId
ã¯ãã€ãã¬ãŒã·ã§ã³æã®äžæŽåãé²ãã€ã€ã¯ã©ã€ã¢ã³ããšãµãŒãã§äžæ㪠ID ãçæããããã®ããã¯ã§ããããã¯äž»ã«ãäžæ㪠ID ãå¿
èŠãšããã¢ã¯ã»ã·ããªã㣠API ãçµã¿èŸŒããããªã³ã³ããŒãã³ãã©ã€ãã©ãªã§æçšãªãã®ã§ããããã«ãã React 17 ããã³ãã以åããæ¢ã«ååšããåé¡ã解決ãããŸãããReact 18 ã§ã¯æ°ããã¹ããªãŒãã³ã°å¯Ÿå¿ã®ãµãŒãã¬ã³ãã©ã HTML ãé çªéãã«éä¿¡ããªããªãããããã®åé¡ã¯ããéèŠã§ãããã¡ãã®ããã¥ã¡ã³ããåç
§ã
è£è¶³
useId
ã¯ãªã¹ãå ã® key ãäœæããã®ã«äœ¿ãããã®ãã®ã§ã¯ãããŸãããkey ã¯ããªãã®ããŒã¿ããäœæãããã¹ãã§ãã
useTransition
useTransition
ãš startTransition
ã«ãããäžéšã®æŽæ°ã¯ç·æ¥æ§ãäœããšããããšãããŒã¯ã§ããããã«ãªããŸãããã®ä»ã®æŽæ°ã¯ããã©ã«ãã§ç·æ¥æ§ãé«ããã®ãšããŠæ±ãããŸããReact ã¯ç·æ¥æ§ã®é«ãæŽæ°ïŒäŸãã°ããã¹ãå
¥åã®æŽæ°ïŒããç·æ¥æ§ã®äœãæŽæ°ïŒäŸãã°æ€çŽ¢çµæã®ãªã¹ãã®ã¬ã³ããŒïŒãäžæã§ããããã«ãªããŸãããã¡ãã®ããã¥ã¡ã³ããåç
§ã
useDeferredValue
useDeferredValue
ã«ãããããªãŒå
ã®ç·æ¥æ§ã®äœãæŽæ°ã®åã¬ã³ããŒãé
延ãããããšãã§ããŸãããããŠã³ã¹ (debounce) ã«äŒŒãŠããŸããããããšæ¯ã¹ãŠããã€ãã®å©ç¹ããããŸããé
延æéãåºå®ã§ãªããããæåã®ã¬ã³ããŒãç»é¢ã«åæ ãããæç¹ã§ããã«é
延ãããŠããæ¹ã®ã¬ã³ããŒãå§ããããã®ã§ãããŸãé
延ãããã¬ã³ããŒã¯äžæå¯èœã§ããããŠãŒã¶ã€ã³ãããããããã¯ããŸããããã¡ãã®ããã¥ã¡ã³ããåç
§ã
useSyncExternalStore
useSyncExternalStore
ã¯ãå€éšã¹ãã¢ãžã®æŽæ°ã匷å¶çã«åæçã«è¡ãããšã§ãå€éšã¹ãã¢ã䞊è¡èªã¿åããè¡ããããã«ããŸããããã«ããå€éšã®ããŒã¿ãœãŒã¹ã«è³Œèªããéã« useEffect
ã䜿ãå¿
èŠæ§ããªããªãã®ã§ãReact å€éšã®ç¶æ
ãæ±ãããããã©ã€ãã©ãªã«ãšã£ãŠæšå¥šããããã®ã§ãããã¡ãã®ããã¥ã¡ã³ããåç
§ã
è£è¶³
useSyncExternalStore
ã¯ã¢ããªã±ãŒã·ã§ã³ã³ãŒãã§ã¯ãªãã©ã€ãã©ãªã§äœ¿çšãããããšãæå³ããŠããŸãã
useInsertionEffect
useInsertionEffect
ã¯ãCSS-in-JS ã©ã€ãã©ãªãã¬ã³ããŒæã«ã¹ã¿ã€ã«ã泚å
¥ããéã®ããã©ãŒãã³ã¹äžã®åé¡ã«å¯ŸåŠã§ããããã«ããããã®æ°ããããã¯ã§ãããã§ã« CSS-in-JS ã©ã€ãã©ãªãæ§ç¯ããŠããã®ã§ãªããã°ãããã䜿ãããšã¯ãŸããªãã§ãããããã®ããã¯ã¯ãDOM ãæžãæããããåŸãã¬ã€ã¢ãŠãå¯äœçš (layout effect) ãæ°ããã¬ã€ã¢ãŠããèªã¿èŸŒãåã«å®è¡ãããŸããããã«ãã React 17 ããã³ãã以åããæ¢ã«ååšããåé¡ã解決ãããŸãããReact 18 ã§ã¯äžŠè¡ã¬ã³ããŒäžã«ãã©ãŠã¶ã«åŠçãæž¡ããããã§ã¬ã€ã¢ãŠããåèšç®ãããå¯èœæ§ããããããããéèŠã§ãããã¡ãã®ããã¥ã¡ã³ããåç
§ã
è£è¶³
useInsertionEffect
ã¯ã¢ããªã±ãŒã·ã§ã³ã³ãŒãã§ã¯ãªãã©ã€ãã©ãªã§äœ¿çšãããããšãæå³ããŠããŸãã
ã¢ããã°ã¬ãŒãæ¹æ³
ã¹ããããã€ã¹ãããã®ã¬ã€ããããã³ç Žå£çå€æŽã»æ³šç®ãã¹ãå€æŽã®å šãªã¹ãã«ã€ããŠã¯ React 18 ã¢ããã°ã¬ãŒãã¬ã€ããåç §ããŠãã ããã
Changelog
React
- Add
useTransition
anduseDeferredValue
to separate urgent updates from transitions. (#10426, #10715, #15593, #15272, #15578, #15769, #17058, #18796, #19121, #19703, #19719, #19724, #20672, #20976 by @acdlite, @lunaruan, @rickhanlonii, and @sebmarkbage) - Add
useId
for generating unique IDs. (#17322, #18576, #22644, #22672, #21260 by @acdlite, @lunaruan, and @sebmarkbage) - Add
useSyncExternalStore
to help external store libraries integrate with React. (#15022, #18000, #18771, #22211, #22292, #22239, #22347, #23150 by @acdlite, @bvaughn, and @drarmstr) - Add
startTransition
as a version ofuseTransition
without pending feedback. (#19696 by @rickhanlonii) - Add
useInsertionEffect
for CSS-in-JS libraries. (#21913 by @rickhanlonii) - Make Suspense remount layout effects when content reappears. (#19322, #19374, #19523, #20625, #21079 by @acdlite, @bvaughn, and @lunaruan)
- Make
<StrictMode>
re-run effects to check for restorable state. (#19523 , #21418 by @bvaughn and @lunaruan) - Assume Symbols are always available. (#23348 by @sebmarkbage)
- Remove
object-assign
polyfill. (#23351 by @sebmarkbage) - Remove unsupported
unstable_changedBits
API. (#20953 by @acdlite) - Allow components to render undefined. (#21869 by @rickhanlonii)
- Flush
useEffect
resulting from discrete events like clicks synchronously. (#21150 by @acdlite) - Suspense
fallback={undefined}
now behaves the same asnull
and isnât ignored. (#21854 by @rickhanlonii) - Consider all
lazy()
resolving to the same component equivalent. (#20357 by @sebmarkbage) - Donât patch console during first render. (#22308 by @lunaruan)
- Improve memory usage. (#21039 by @bgirard)
- Improve messages if string coercion throws (Temporal.*, Symbol, etc.) (#22064 by @justingrant)
- Use
setImmediate
when available overMessageChannel
. (#20834 by @gaearon) - Fix context failing to propagate inside suspended trees. (#23095 by @gaearon)
- Fix
useReducer
observing incorrect props by removing the eager bailout mechanism. (#22445 by @josephsavona) - Fix
setState
being ignored in Safari when appending iframes. (#23111 by @gaearon) - Fix a crash when rendering
ZonedDateTime
in the tree. (#20617 by @dimaqq) - Fix a crash when document is set to
null
in tests. (#22695 by @SimenB) - Fix
onLoad
not triggering when concurrent features are on. (#23316 by @gnoff) - Fix a warning when a selector returns
NaN
. (#23333 by @hachibeeDI) - Fix a crash when document is set to
null
in tests. (#22695 by @SimenB) - Fix the generated license header. (#23004 by @vitaliemiron)
- Add
package.json
as one of the entry points. (#22954 by @Jack) - Allow suspending outside a Suspense boundary. (#23267 by @acdlite)
- Log a recoverable error whenever hydration fails. (#23319 by @acdlite)
React DOM
- Add
createRoot
andhydrateRoot
. (#10239, #11225, #12117, #13732, #15502, #15532, #17035, #17165, #20669, #20748, #20888, #21072, #21417, #21652, #21687, #23207, #23385 by @acdlite, @bvaughn, @gaearon, @lunaruan, @rickhanlonii, @trueadm, and @sebmarkbage) - Add selective hydration. (#14717, #14884, #16725, #16880, #17004, #22416, #22629, #22448, #22856, #23176 by @acdlite, @gaearon, @salazarm, and @sebmarkbage)
- Add
aria-description
to the list of known ARIA attributes. (#22142 by @mahyareb) - Add
onResize
event to video elements. (#21973 by @rileyjshaw) - Add
imageSizes
andimageSrcSet
to known props. (#22550 by @eps1lon) - Allow non-string
<option>
children ifvalue
is provided. (#21431 by @sebmarkbage) - Fix
aspectRatio
style not being applied. (#21100 by @gaearon) - Warn if
renderSubtreeIntoContainer
is called. (#23355 by @acdlite)
React DOM Server
- Add the new streaming renderer. (#14144, #20970, #21056, #21255, #21200, #21257, #21276, #22443, #22450, #23247, #24025, #24030 by @sebmarkbage)
- Fix context providers in SSR when handling multiple requests. (#23171 by @frandiox)
- Revert to client render on text mismatch. (#23354 by @acdlite)
- Deprecate
renderToNodeStream
. (#23359 by @sebmarkbage) - Fix a spurious error log in the new server renderer. (#24043 by @eps1lon)
- Fix a bug in the new server renderer. (#22617 by @shuding)
- Ignore function and symbol values inside custom elements on the server. (#21157 by @sebmarkbage)
React DOM Test Utils
- Throw when
act
is used in production. (#21686 by @acdlite) - Support disabling spurious act warnings with
global.IS_REACT_ACT_ENVIRONMENT
. (#22561 by @acdlite) - Expand act warning to cover all APIs that might schedule React work. (#22607 by @acdlite)
- Make
act
batch updates. (#21797 by @acdlite) - Remove warning for dangling passive effects. (#22609 by @acdlite)
React Refresh
- Track late-mounted roots in Fast Refresh. (#22740 by @anc95)
- Add
exports
field topackage.json
. (#23087 by @otakustay)