简体   繁体   中英

When data is changed, doesn't update on UI but seen in console

In a React project, with certain records of input fields, date data can be changed, but, doesn't reflect it on UI and when seen in console, its shown over there. I'm using react datepicker for date component. Please refer to code below:

const [startDate, setStartDate] = useState();

{colConfig[cIndex].data_type === "date" &&
   !colConfig[cIndex].cell_click_callback && (
   <div>
   <DatePickerNew
   setRequesterDate={(e) =>
   dateCallback({dateVal: e, id: rowData[0].id})}
      startDate={colData}
      setStartDate={setStartDate}
      />
   </div>
)}

As you can see from above code, I'm passing 'coldata' from props and accordingly displaying. The data doesn't get updated on UI but, its seen in console.

You can see from image below, the date data is changed for 'Shawns' but it isn't updated on UI, but, can be seen in console. What could be the best solution to sort it out?

在此处输入图像描述

Please refer to codesandbox link --> https://codesandbox.io/s/elated-varahamihira-xpjtdb?file=/src/Grid.js:499-544

The problem is, you set your startDate properly but never use it to display the data.

If you just use startDate in you component instead of colData , you will see the changes starting to reflect properly:

 <DatePickerNew
   setRequesterDate={(e) =>
     dateCallback({dateVal: e, id: rowData[0].id})
   }
   startDate={startDate} // <- Using startDate here
   setStartDate={setStartDate}
 />

You would obviously need state for each row seperately which would be much better if you have the startDate state inside the DatePickerNew component

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM