Useref

Sainath KadamSainath Kadam
1 min read

using this we can directly manipulate the DOM.

using this how? when?
-> if we have to track the initial value and current value like when we want to do the reset button that time we can use the useref insead of using the useState.

eg.

initialise first.

  const initialPlanDataRef = useRef<IPlanData | null>(null);

the set the value in the use-ref

const fetchedPlanData = response?.data;
          setPlanData(fetchedPlanData);
          initialPlanDataRef.current = { ...fetchedPlanData };

when we want to reset the data whatever we have added.

const handleReset = () => { if (initialPlanDataRef.current) { setPlanData({ ...initialPlanDataRef.current }); setIsDataModified(false); } };

1
Subscribe to my newsletter

Read articles from Sainath Kadam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sainath Kadam
Sainath Kadam

I am software engineer from India.