Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

componentWillUnmount

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {    this.setState({      date: new Date()    });  }
  render() {
    return (
      <div>
        <h1>Bonjour, monde !</h1>
        <h2>Il est {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

ReactDOM.render(
  <Clock />,
  document.getElementById('root')
);
Comment

component will unmount

import React from 'react';
class ComponentOne extends React.Component {
 
  // Defining the componentWillUnmount method
  componentWillUnmount() {
    alert('The component is going to be unmounted');
  }
 
  render() {
    return <h1>Hello Geeks!</h1>;
  }
}
 
class App extends React.Component {
  state = { display: true };
  delete = () => {
    this.setState({ display: false });
  };
 
  render() {
    let comp;
    if (this.state.display) {
      comp = <ComponentOne />;
    }
    return (
      <div>
        {comp}
        <button onClick={this.delete}>
          Delete the component
        </button>
      </div>
    );
  }
}
 
export default App;
Comment

how does did componentmount work

    class Example extends React.Component {
        componentWillMount() {
            console.log('I am about to say hello');
        }

        render() {
            return <h1>Hello world</h1>;
        }
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: random number generator javascript with range 
Javascript :: make form submit on new tab using jquery 
Javascript :: How to get current time zone in javascript 
Javascript :: react bootstrap form select 
Javascript :: formdata array of objects 
Javascript :: select in react js 
Javascript :: get user time using timezone javascript 
Javascript :: react native webview not working 
Javascript :: remove all duplicates from an array 
Javascript :: express router file 
Javascript :: Link vs NavLink in react-router-dom 
Javascript :: storage class 
Javascript :: javascript creeate utc date 
Javascript :: javascript generator function 
Javascript :: adding element to javascript object 
Javascript :: vue js cdn 
Javascript :: create a json object in javascript 
Javascript :: mongoose pagination with total count 
Javascript :: tsconfig 
Javascript :: discord.js v13 if dm 
Javascript :: pattern validator angular 
Javascript :: uppercase javascript using function 
Javascript :: js math function that returns smallest value 
Javascript :: regex js pattern tags 
Javascript :: MongoDb user find 
Javascript :: mapbox remove marker 
Javascript :: polyfill of bind 
Javascript :: split array into chunks javascript 
Javascript :: how to repeat an array of objects n times in javascript 
Javascript :: check object is null empty or undefined 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =