react class component example with state

Example 1: how to write statefull class in react

import React, {Component} from 'react'

class Pigeons extends Component {
  constructor() {
    super()
    this.state = {
      pigeons: []
    }
  }
  render() {
    return (
      

Look at all the pigeons spotted today!

    {this.state.pigeons.map(pigeonURL => { return
  • })}
) } }

Example 2: state with react functions

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  render() {
    return (
      

You clicked {this.state.count} times

); } }

Example 3: setstate react js

constructor(props) {
        super(props);
        this.state = {
            isActive: true,
        };
    }

    checkStatus = () => {
        this.setState({		// use this function
            'isActive' : !this.state.isActive,
        });
    }

Example 4: reading state react

const Example = (props) => {
  // You can use Hooks here!
  return 
; }

Tags:

Misc Example