simple counter react code example

Example 1: simple counter react

import React, { useState } from 'react';
import ReactDOM from 'react-dom';

export const Counter = (props) => {
  const [count, setCount ] = useState(0)

  return (
    
button count: {count}
); } ReactDOM.render( , document.getElementById('root') );

Example 2: simple counter react

import React from 'react';
import ReactDOM from 'react-dom';

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

  increment() {
    this.setState({count:this.state.count+1});
  }

  render() {
    return (
      
button count: {this.state.count}
); } } ReactDOM.render( , document.getElementById('root') );

Tags:

Misc Example