list of react hooks code example

Example 1: useRef

/*
	A common use case is to access a child imperatively: 
*/

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>
    </>
  );
}

Example 2: react useeffect

import React, { useEffect } from 'react';
	
	const [data,setData]=useState()
    useEffect(() => {
        fetch(`put your url/api request in here`)
        .then(res=>res.json())
        .then(json => setData(json))
    },[])

Example 3: reference hook in react

import React, {useRef} from 'react'
const displayNode = useRef(null)
//action
displayNode.current.textContent = `${count} Appointments Successfully Uploaded`
//referenced object
<div ref={displayNode} className="text-center mt-3 msg" style={{ color: "green", fontWeight: "800" }}></div>

Example 4: react hooks

const [state, setState] = useState(initialState);

Example 5: usestate

const [count, setCount] = useState(0);