Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

useref hook react

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

function App() {
  const intervalRef = useRef()
  const [count, setCount] = useState(0)

  useEffect(() => {
    intervalRef.current = setInterval(() => setCount(count => count + 1), 1000)

    return () => {
      clearInterval(intervalRef.current)
    }
  }, [])

  return (
    <>
      <div style={{ fontSize: 120 }}>{count}</div>
      <button
        onClick={() => {
          clearInterval(intervalRef.current)
        }}
      >
        Stop
      </button>
    </>
  )
}

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

useref in functional component

import React, { useRef } from 'react';

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}
Comment

React useRef Hook

//Use useRef to track application renders
import { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom/client";

function App() {
  const [inputValue, setInputValue] = useState("");
  const count = useRef(0);

  useEffect(() => {
    count.current = count.current + 1;
  });

  return (
    <>
      <input
        type="text"
        value={inputValue}
        onChange={(e) => setInputValue(e.target.value)}
      />
      <h1>Render Count: {count.current}</h1>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
Comment

PREVIOUS NEXT
Code Example
Javascript :: vue js count down timer 
Javascript :: difference between package.json and package lock.json 
Javascript :: tableau js 
Javascript :: axios react post form data 
Javascript :: dynamic array in javascript 
Javascript :: for:each in lwc js 
Javascript :: js get each pair of values from an array of objects 
Javascript :: feet to mile js 
Javascript :: express.js get params 
Javascript :: how to handle error js 
Javascript :: datatable set row id 
Javascript :: javascript find the longest word in a string 
Javascript :: javascript get smaller of two numbers 
Javascript :: node js do request 
Javascript :: removeitem localstorage 
Javascript :: moment clone 
Javascript :: convert string to lowercase javascript 
Javascript :: vue computed 
Javascript :: html get form elements 
Javascript :: moment time from now 
Javascript :: javascript parsefloat 
Javascript :: js iife 
Javascript :: what is redis 
Javascript :: apollo clear cache for query 
Javascript :: function with for loop 
Javascript :: range in javascript 
Javascript :: js group objects in array 
Javascript :: js get request 
Javascript :: super class js 
Javascript :: Run project in visual studio with iis express 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =