React componentDidMount() メソッド

React 组件生命周期React コンポーネントのライフサイクル

componentDidMount() メソッドの書式は次のとおりです。

componentDidMount()

componentDidMount() メソッドは、コンポーネントがマウントされた後(DOM ツリーに挿入された後)にすぐに呼び出されます。

DOM ノードに依存する初期化は、componentDidMount() メソッド内に置く必要があります。

次の例では、最初に出力しますサンプル、その後componentDidMount()メソッドを使用して、コンポーネントのマウント後に出力するように設定します。google:

例

class Header extends React.Component { constructor(props) { super(props); this.state = {favoritesite: "サンプル"}; } componentDidMount() { setTimeout(() => { this.setState({favoritesite: "google"}) }, 1000) } render() { return ( <h1>私の好きなウェブサイトは{this.state.favoritesite}</h1> ); } }

試してみる »

React 组件生命周期React コンポーネントのライフサイクル