list of react hooks code example
Example 1: useRef
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
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)
displayNode.current.textContent = `${count} Appointments Successfully Uploaded`
<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);