YouTip LogoYouTip

React Ref Componentdidmount

# React componentDidMount() Method [![Image 2: React Component Lifecycle](#) React Component Lifecycle](#) The componentDidMount() method format is as follows: componentDidMount() The componentDidMount() method is called immediately after a component is mounted (inserted into the DOM tree). Initialization that depends on DOM nodes should be placed in the componentDidMount() method. The following example will first output ****, then use the componentDidMount() method to set it to output **google** after the component is mounted: ## Example ```class Header extends React.Component{ constructor(props){ super(props); this.state = {favoritesite: ""}; } componentDidMount(){ setTimeout(() =>{ this.setState({favoritesite: "google"}) }, 1000) } render(){ return(

My favorite website is {this.state.favoritesite}

); } } [Try it Β»](#) [![Image 3: React Component Lifecycle](#) React Component Lifecycle](#)
← React Ref GetsnapshotbeforeupdReact Ref Getderivedstatefromp β†’