Reactガイドを読んでいくその91

これは

Reactのガイドを読んでいく記事です。

ガイドのリンク

ja.reactjs.org

RefとDOM

クラスコンポーネントへの Ref の追加

マウント直後にクリックされることをシミュレーションするためにラップしたい場合、
refを使用してカスタムインプットにアクセスすることでメソッドを手動で呼び出すことができる。

>引用
class AutoFocusTextInput extends React.Component {
  constructor(props) {
    super(props);
    this.textInput = React.createRef();
  }

  componentDidMount() {
    this.textInput.current.focusTextInput();
  }

  render() {
    return (
      <CustomTextInput ref={this.textInput} />
    );
  }
}

このような場合、CustomTextInputがクラスとして宣言されていることが必須となるので注意が必要。

今日はここまで。