Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

counter app in react class based component

import React, { Component } from 'react'

export default class CounterAppClass extends Component {

    constructor(props) {
        super();
        this.state = {
            count: 0
        }
    }

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

    render() {
        return (
            <div className='card'>
                <div className="card-body">
                    <h3 className='text-center mb-2'>Class based Component Counter</h3>
                    <h5>Count is: {this.state.count}</h5>
                    <button className='btn btn-primary w-100 mt-3' onClick={this.handleIncrease.bind(this)}>Increase</button>
                </div>
            </div>
        )
    }
}
Comment

counter example using classes react without jsx

<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<div id='root'></div>
<script>
class Hello extends React.Component {
 constructor(props) {
        super(props)
        this.state = {
            count: 0
        }
    }

  render() {
    return React.createElement('div', null, React.createElement("button",{onClick: () => this.setState({ count: this.state.count + 1})}, "Inc "+ this.state.count));
  }
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(React.createElement(Hello, {toWhat: 'World'}, null));
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: adding javascript object within array in the middle position 
Javascript :: for-loop-how-to-loop-through-an-array-in-js 
Javascript :: javascript for backend 
Javascript :: Solution-2--solution options for reverse bits algorithm js 
Javascript :: How to fix prettier messing up your HTML on save 
Javascript :: array of objects javascript 
Javascript :: angular button click event 
Javascript :: javascript tree search 
Javascript :: react tutorial app 
Javascript :: js function to print word starts with vowels of given string 
Javascript :: or js 
Javascript :: table to excel javascript 
Javascript :: moment max 
Javascript :: comment field react 
Javascript :: javascript number with commas 
Javascript :: lodash sum array of objects 
Javascript :: instance in javascript 
Javascript :: microbit hello world 
Javascript :: js how to get n fibonacci number 
Javascript :: print blade value in js 
Javascript :: javascript prototype chaining 
Javascript :: javascript Set Subset Operation 
Javascript :: electron webcontent send data into react not working 
Javascript :: disable SerializableStateInvariantMiddleware and ImmutableStateInvariantMiddleware middlewares for large redux stores 
Javascript :: javascript process.env.key with  
Javascript :: get page scrolling amount js 
Javascript :: phaser random triangle 
Javascript :: How to call the API when the search value changes 
Javascript :: when end sound show alert 
Javascript :: javascript to jquery code converter online 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =