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 :: round 2 decimal places 
Javascript :: Check propery of an objects array 
Javascript :: mariadb JSON_ARRAYAGG does not exist 
Javascript :: how to find missing number in integer array of 1 to 100 in javascript 
Javascript :: jquery autocomplete bootstrap modal 
Javascript :: mongoose select 
Javascript :: how to create a slice of the array with n elements taken from the beginning in javascript 
Javascript :: how can you set an environment variable in node 
Javascript :: javascript set cookie 
Javascript :: pull out only text from element javascript 
Javascript :: How to add JSX elements in an array 
Javascript :: what is after.js 
Javascript :: remove duplicates javascript 
Javascript :: node js if no arguments 
Javascript :: how to handle errors with xmlhttprequest 
Javascript :: how to see my timezone using js 
Javascript :: js content editable 
Javascript :: for each array 
Javascript :: js initialize 2d array 
Javascript :: get width of screen 
Javascript :: how to make a dictionary javascript 
Javascript :: is dark mode 
Javascript :: @angular-devkit/build-angular <error 
Javascript :: clearinterval in javascript 
Javascript :: angular router link 
Javascript :: current date jquery and current day 
Javascript :: electron js web reference to use node 
Javascript :: react navbar material ui 
Javascript :: axios post request progress 
Javascript :: navigate json object javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =