Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

counter with react hooks

import React, { useState } from 'react';

function Example() {
  // Declaración de una variable de estado que llamaremos "count"  const [count, setCount] = useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
Comment

automated counter with react hooks

const { useState, useEffect, useRef } = React;

function useInterval(callback, delay) {
  const savedCallback = useRef();

  // Remember the latest callback.
  useEffect(() => {
    savedCallback.current = callback;
  }, [callback]);

  // Set up the interval.
  useEffect(() => {
    let id = setInterval(() => {
      savedCallback.current();
    }, delay);
    return () => clearInterval(id);
  }, [delay]);
}

function App() {
  const [counter, setCounter] = useState(0);

  useInterval(() => {
    setCounter(counter + 1);
  }, 1000);

  return <h1>{counter}</h1>;
};

ReactDOM.render(
  <App />,
  document.getElementById('root')
);
Comment

simple counter with react hook

import { useState } from "react";

function App() {
  return (
    <div className="App">
      <Counter></Counter>
    </div>
  );
}

function Counter() {
  const [count, setCount] = useState(0);
  const increaseCount = () => setCount(count + 1);
  const decreaseCount = () => setCount(count - 1);
  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={increaseCount}>Increase</button>
      <button onClick={decreaseCount}>Decrease</button>
    </div>
  );
}

export default App;

Comment

PREVIOUS NEXT
Code Example
Javascript :: load images js context 
Javascript :: how to define cardTitle background image in mdl in reactjs 
Javascript :: how to prevent render in react 
Javascript :: create slug using middleware 
Javascript :: regular expression javascript 
Javascript :: js !! 
Javascript :: codeceptjs "waitForClickable" 
Javascript :: monaco editor cdn 
Javascript :: js addeventlistener input searcb mobile 
Javascript :: javascript string to boolean 
Javascript :: check if class is clicked javascript 
Javascript :: rename column infotable thingworx 
Javascript :: Delete a user in ExpressJS 
Javascript :: change element text innerhtml keeping the elements or tags inside 
Javascript :: rounding to two decimal places 
Javascript :: calculate time in seconds javascript angular 
Javascript :: js recursive fetch 
Javascript :: #{} js 
Javascript :: javascript Sum of all the factors of a number 
Javascript :: form serialze submit button 
Javascript :: javascript change first letter to uppercase 
Javascript :: componentdidmount react hooks 
Javascript :: what is == in js 
Javascript :: classic asp json multidemsion json 
Javascript :: set time slots with date in javascript 
Javascript :: copy svg to clipboard javascript 
Javascript :: angular playground online 
Javascript :: js promisify function 
Javascript :: perfect scrollbar jquery 
Javascript :: javascript select letter in string 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =