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 :: anonymous function javascript 
Javascript :: dynamic array in javascript 
Javascript :: jsjs trigger window error 
Javascript :: vue displaying a this.length 
Javascript :: trigger keyboard event javascript 
Javascript :: Get the current tab 
Javascript :: javascript submit form programmatically 
Javascript :: fizz buzz program in javascript 
Javascript :: firebase.apps.length 
Javascript :: select parent of elemt 
Javascript :: JavaScript grouping words by length 
Javascript :: javascript get smaller of two numbers 
Javascript :: npm get package.json version 
Javascript :: Self Invoking Function Simpler Syntax 
Javascript :: express.js hello world 
Javascript :: scroll for sticky 
Javascript :: An invalid form control with ... is not focusable. 
Javascript :: remove array item with index 
Javascript :: mongoose bulk create 
Javascript :: react native flatlist container style 
Javascript :: mongoose find get nested prop only 
Javascript :: plotly express bar graph 
Javascript :: make a bot send a welcome message discordjs 
Javascript :: clone element 
Javascript :: Object.values returns 
Javascript :: javascript kill all childs 
Javascript :: javascript unicode to string 
Javascript :: angular how to run code every time you route 
Javascript :: convert html to docx javascript 
Javascript :: jquery select selected get data name 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =