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 :: find a word in string javascript 
Javascript :: form submit event get button 
Javascript :: regex js pattern tags 
Javascript :: how to filter json array in javascript 
Javascript :: sfc in react 
Javascript :: how to add checked in javascript 
Javascript :: how to get range slider value in javascript 
Javascript :: merge 2 json objects js 
Javascript :: sort in javascript array 
Javascript :: javascript time 
Javascript :: linking html with javascript 
Javascript :: react native red triangle up 
Javascript :: How to get latitude and longitude from address in angular 6 
Javascript :: electron new window 
Javascript :: uncaught (in promise): both the table and dtoptions cannot be empty 
Javascript :: Difference in push and navigate in react Navigation 
Javascript :: useeffect react example 
Javascript :: react promises 
Javascript :: import library react js 
Javascript :: mongo mongoose join aggregation lookup 
Javascript :: get image as blob 
Javascript :: json_insert mysql 
Javascript :: fahrenheit to celsius in javascript 
Javascript :: how to run react builed version 
Javascript :: nanoid 
Javascript :: how can when click buton scrool to another elemtn 
Javascript :: image react native 
Javascript :: javascript default parameters 
Javascript :: Multiple children were passed to <Link with `href` of `/escritorio/opcion1` but only one child is supported 
Javascript :: fuse.js 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =