Skip to content

Commit

Permalink
Improve useRef.
Browse files Browse the repository at this point in the history
  • Loading branch information
elarsaks committed Aug 3, 2023
1 parent c370ff4 commit b821f25
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/pages/UseRef.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import util from "../styles/Util.module.css";
export default () => {
const [name, setName] = useState(() => "");
const renderCount = useRef(0);
const prevName = useRef("");

useEffect(() => {
prevName.current = name;
renderCount.current = renderCount.current + 1;
});

Expand All @@ -23,11 +25,17 @@ export default () => {
<input
type="text"
value={name}
placeholder="Enter your name"
className={util["input"]}
onChange={(e) => setName(e.target.value)}
/>
<h2 className={`${info["info"]} `}>
My name is <span> {name ? name : "...."} </span> <br></br>
My name is <span> {name ? name : "_"} </span> <br></br>
It used to be <span>
{" "}
{prevName.current ? prevName.current : "_"}{" "}
</span>{" "}
<br></br> <br></br>
Componenty was rendered <span> {renderCount.current} </span> times.
</h2>

Expand Down

0 comments on commit b821f25

Please sign in to comment.