React Hooks ã使ã£ãã¢ããªã§ãã¿ã³ã®ç¶æ
ããã£ã¼ã«ãã¸ã®ãã©ã¼ã«ã¹ãªã© ref
ã使ã£ãæä½ãããã®ãã¿ã³ããã£ã¼ã«ãã®è¦ªã³ã³ãã¼ãã³ãããå¼ã³åºãããæã®ã¡ã¢ã
æ¥æ¬èªãä¸èªç±ãªã®ã§ãµã³ãã«ã
ä¾ãã°ãã¿ã³ãæ¼ããæã«éåæå¦çããããããå¦çãå®äºããã¾ã§ãã¿ã³ã disabled
ã«ãã¦ããããã¿ãããªæ
åç´ã« useRef ãããã® props ã§æ¸¡ãã¦ãä¸æããããªã
import React, { useRef } from 'react'; import SubmitButton from './SubmitButton'; function PostList(props) { const submitBtn = useRef(null); const onSubmitHandler = async (e) => { submitBtn.current.disabled = true; try { // ...éåæå¦ç } catch (err) { ... } submitBtn.current.disabled = false; }; return ( <> {/* ...å¦ç */} <SubmitButton ref={submitBtn} onClick={onSubmitHandler} /> </> ); }
SubmitButton.js
import React from 'react'; function SubmitButton(props) { return ( <button className="p-submitBtn" ref={props.ref} onClick={props.onClick} />SUBMIT</button> ); } export default SubmitButton;
useRef
ãããã®ã props
ã§æ¸¡ãã¦ãã¿ã³ã® ref
å±æ§ã«ä»ãã¦ã親ã³ã³ãã¼ãã³ãã§ã¯ null
ã«ãªã£ã¦ãã¾ããã¾ããããªãã
ä¸è¨ã®ä¾ã§ã¯ãã¿ã³ãæ¼ããæã«å®è¡ããã onSubmitHandler
å
㧠submitBtn.current
ã null
ãªã®ã§ã¨ã©ã¼ã«ãªã£ã¦ãã¾ãæå³ããã¨ããã«åä½ããªãã
const onSubmitHandler = async (e) => { // submitBtn.current 㯠null submitBtn.current.disabled = true; // ... };
Forwarding Refs ã使ã
useImperativeHandle
customizes the instance value that is exposed to parent components when usingref
. As always, imperative code using refs should be avoided in most cases.useImperativeHandle
should be used withforwardRef
ref. Hooks API Reference â React #useimperativehandle
React Hooks ã§ã¯åã³ã³ãã¼ãã³ãå
㧠useImperativeHandle
ã使ã ref.current
ããå¼ã³åºããé¢æ°ãå®ç¾©ãã¦ãåã³ã³ãã¼ãã³ãèªä½ã forwardRef()
ã§ã«æ¸¡ãã¦ãããã¨ã§è¦ªã³ã³ãã¼ãã³ãã®ref.current
ãéãã¦åã³ã³ãã¼ãã³ãã® ref
ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã
import React, { useRef } from 'react'; import SubmitButton from './SubmitButton'; function PostList(props) { const submitBtn = useRef(null); const onSubmitHandler = async (e) => { // ref.current ãéãã¦é¢æ°ãå¼ã³åºãããã«å¤æ´ submitBtn.current.disabled(false); try { // ...éåæå¦ç } catch (err) { ... } submitBtn.current.disabled(false); }; return ( <> {/* ...å¦ç */} <SubmitButton ref={submitBtn} onClick={onSubmitHandler} /> </> ); }
SubmitButton.js
import React, { useRef, forwardRef, useImperativeHandle } from 'react'; function SubmitButton(props, ref) { const btnRef = useRef(); useImperativeHandle(ref, () => { // 親ã³ã³ãã¼ãã³ãã® ref.current ããå®è¡ã§ããé¢æ°ãå®ç¾©ãããªãã¸ã§ã¯ããè¿ã return { disabled: (flg) => btnRef.current.disabled = !!flg } }); return ( <button className="p-submitBtn" ref={btnRef} onClick={props.onClick} />SUBMIT</button> ); } // ã³ã³ãã¼ãã³ãã forwardRef ã§ã©ãã SubmitButton = forwardRef(SubmitButton); export default SubmitButton;
forwardRef
ã使ãå ´å const
ã§é¢æ°å®ç¾©ããã»ããè¦ãããããï¼
const SubmitButton = React.forwardRef((props, ref) => { // ... });
ããã§ãã¿ã³ãã¯ãªãã¯ããæã«ãéåæå¦çãå®äºããã¾ã§ãã¿ã³ã disabled
ã§ããããã«ãªãã¾ããã
ãã¨ã
useImperativeHandle
ã§è¿ããªãã¸ã§ã¯ãã« setter
ã¨ãã¦é¢æ°ãå®ç¾©ããã親ã³ã³ãã¼ãã³ãããããããã£ã¿ããã«ã¢ã¯ã»ã¹ã§ããã®ããªï¼ ãã§ããæ°ã«ãªã£ã¦ããã®ã§ãæéãããã¨ãã«ã§ã試ãã¦ã¿ããã
react-create-app
ãããã©ã«ã㧠Function Component ã«ãªã£ãããGWä¸ã«ã ã㶠React Hooks ã¨ä»²è¯ããªããã®ã§ãã¾ã¨ãããã®ãè¿ã
æ¸ãã¦ããããã (ã¾ã 触ãã¦ãªã Hooks ããããããããã©â¦
[åè]
- ä½è :ç©´äº å®å¹¸,ç³äº ç´ç¢,æ´ç° åç¥,ä¸å®® è
- çºå£²æ¥: 2018/02/19
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼