react constructor code example

Example 1: react native class component constructor

import React from 'react';  
import { View, TextInput } from "react-native";

class App extends React.Component {  
  constructor(props){  
    super(props);  
    this.state = {  
         name: "" 
      }  
    this.handleChange = this.handleChange.bind(this);  
  }
  handleChange(text){
    this.setState({ name: text })
    console.log(this.props);  
  }  
  render() {  
    return (  
    
      this.handleChange(text)}
      />
      
  }  
}  
export default App;

Example 2: constructor react

constructor(props) {
  super(props);
  // Don't call this.setState() here!
  this.state = { counter: 0 };
  this.handleClick = this.handleClick.bind(this);
}

Example 3: lifecycles if reactjs

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

  componentDidMount() {  }
  componentWillUnmount() {  }
  render() {
    return (
      

Hello, world!

It is {this.state.date.toLocaleTimeString()}.

); } }

Example 4: shouldcomponentupdate

shouldComponentUpdate(nextProps, nextState) {
  return true;
}

Example 5: component did mmount

componentDidMount()

Example 6: component did mmount

componentDidUpdate(prevProps, prevState, snapshot)

Tags:

Misc Example