Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react state hooks

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count"  
  const [count, setCount] = useState(0);
  
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
Comment

usestate hook react

import React, { useState } from 'react'
import { render } from 'react-dom'

const randomDiceRoll = () => {
  return Math.floor(Math.random() * 6) + 1
}

export default function App() {
  const [diceRolls, setDiceRolls] = useState([1, 2, 3])

  return (
    <div>
      <button
        onClick={() => {
          setDiceRolls([...diceRolls, randomDiceRoll()])
        }}
      >
        Roll dice
      </button>
      <ul>
        {diceRolls.map((diceRoll, index) => (
          <li key={index}>{diceRoll}</li>
        ))}
      </ul>
    </div>
  )
}

render(<App />, document.querySelector('#app'))
Comment

usestate react

import React, { useState } from 'react';

function Example() {
	
  const [count, setCount] = useState(0);
  
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
Comment

react hook usestate

function Counter({initialCount}) {
  const [count, setCount] = useState(initialCount);
  return (
    <>
      Count: {count}
      <button onClick={() => setCount(initialCount)}>Reset</button>
      <button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
      <button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
    </>
  );
}
Comment

React useState Hook

import { useState } from "react"
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript undefined check 
Javascript :: jquery close another dialog 
Javascript :: next js absolute path 
Javascript :: js localstorage 
Javascript :: delay statement in js 
Javascript :: javascript es6 check if index exists 
Javascript :: check palindrome javascript 
Javascript :: angular datatable reload with pagination 
Javascript :: vue.js textbox 
Javascript :: make contenteditable false javascript 
Javascript :: javascript sum of array 
Javascript :: handle error in promise chain 
Javascript :: jquery detect change in textarea content 
Javascript :: control audio javascript 
Javascript :: hello word in js 
Javascript :: regExp numero français 
Javascript :: html add class 
Javascript :: insert image into datatable 
Javascript :: rename file in js 
Javascript :: npx create-react-app current folder 
Javascript :: how to routing in react js 
Javascript :: usestate remove from array 
Javascript :: allow only numerics and few alphabets in input type js 
Javascript :: queryselectorall data attribute 
Javascript :: javascript separate string by char 
Javascript :: how to check the extension of a file in javascript 
Javascript :: copy to clipboard angular 
Javascript :: javascript console group 
Javascript :: hwo to cehck req header in js 
Javascript :: nodejs date difference 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =