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 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

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

what does useref do react

const refContainer = useRef(initialValue);
//useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). 
//The returned object will persist for the full lifetime of the component.
Comment

what is useref in react

useRef is an kind of alternative to useState hook , with useRef you can get an reference to  an element and than use the DOM/JS properties to modify it .
You can even call useRef as an tool which is used to apply all the DOM properties as you were doing in Javascript before coming to React.
If you use useRef than this  apparoch is called un-controlled components.
Comment

useref in react

import { useRef } from 'react';

function LogButtonClicks() {
  const countRef = useRef(0);  
  const handle = () => {
    countRef.current++;    console.log(`Clicked ${countRef.current} times`);
  };

  console.log('I rendered!');

  return <button onClick={handle}>Click me</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 :: react-native-community/blur 
Javascript :: select the first elemnt have class in jquery 
Javascript :: extract string from string javascript based on word 
Javascript :: loop javascript 
Javascript :: array index javascript show only first 2 elements 
Javascript :: puppeteer headless ubuntu server install required 
Javascript :: jquery values to array 
Javascript :: react media recoder 
Javascript :: last item in array javascript 
Javascript :: javascript create an array 
Javascript :: add in to array mongoose 
Javascript :: JavaScript find the shortest word in a string 
Javascript :: add button dynamically in javascript 
Javascript :: javascript string to array 
Javascript :: p5js left mouse click 
Javascript :: constructor function 
Javascript :: javascript array flatten 
Javascript :: find class 
Javascript :: urlsearchparams to object 
Javascript :: count in string javascript 
Javascript :: mongoose filter 
Javascript :: change react native app name 
Javascript :: js stringify 
Javascript :: used to retrieve dat from firebase realtime datastore 
Javascript :: random function javascript 
Javascript :: convert string time to date time object 
Javascript :: js filter array of objects by another object 
Javascript :: break statement in javascript 
Javascript :: to the power of javascript 
Javascript :: js library for unique id uniqid 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =