mui-datatables icon indicating copy to clipboard operation
mui-datatables copied to clipboard

updateValue in customBodyRender doesn't work when used with useState

Open jaekunchoi opened this issue 4 years ago • 7 comments

updateValue in customBodyRender doesn't update value when setting values via useState.

Expected Behavior

updateValue updates the row properly when used with useState

Current Behavior

updateValue doesn't update value and the row is not changed in customBodyRender

Steps to Reproduce (for bugs)

Below is an example to highlight the problem. I did not put entire code as they are just distractions for now.

customBodyRender: (value, tableMeta, updateValue) => {
    const handleSearch = () => {
            const monthlySearches = tableMeta.rowData[1];
            setIsEvaluating(true);
            setClickedRowIndex(tableMeta.rowIndex);
            updateValue(true);
            setTest('hello');
            ApiClient.get(`/test`, { params: { historicalVolume: monthlySearches } })
              .then(res => {
                updateValue(res.history);
                setSearches(searches);
              })
              .catch(err => {
                updateValue(null)
                if (err.status === 422) {
                  setErrorMsg(`${err.data}`);
                  setAlertSeverity('error');
                  setOpenErrorMsg(true);
                }
              })
              .finally(() => {
                setIsEvaluating(false);
              });
          };

          return (
            <Tooltip title={tooltip}>
              <div className={classes.root}>
                <div className={classes.wrapper}>
                  { Number.isInteger(value) ? 
                    <IconButton aria-label="evaluate" variant="contained" size="small" onClick={handleSearch} disabled={isEvaluating===true}>
                      <Chip size="small" className={difficultyClassname} label={value} />
                    </IconButton> 
                    :
                    <IconButton aria-label="evaluate" variant="contained" size="small" onClick={handleSearch} disabled={isEvaluating===true}>
                      <SearchIcon color="primary" />
                    </IconButton> 
                  }
                  { tableMeta.rowIndex === clickedRowIndex && isEvaluating && <CircularProgress size={32} className={classes.buttonProgress} /> }
                </div>
              </div>
            </Tooltip>
          )
}

Is it possible to mix both and still use updateValue under customBodyRender?

jaekunchoi avatar Apr 27 '20 17:04 jaekunchoi

why do you need to update some state and make an api call while the datatable cell is being painted? can you give some extra details?

hasanbisha avatar May 04 '20 14:05 hasanbisha

It needs to be able to call api when the cell is clicked and update the clicked cell with api response data

jaekunchoi avatar May 04 '20 16:05 jaekunchoi

yes you can use it, does it throw any error?

hasanbisha avatar May 04 '20 17:05 hasanbisha

I could investigate further but I'd need a codesandbox showing the issue.

patorjk avatar Jun 09 '20 17:06 patorjk

Any update about it we are also facing the same issue, The state is not getting updated when used with updateValue original state persists and not sure of how to fetch the updated data of the table

anudeepjusanu avatar Aug 19 '20 02:08 anudeepjusanu

yeah, facing the same issue here. It's kinda of a deal breaker for what I need to achieve right now

Instrumedley avatar Jul 18 '21 20:07 Instrumedley

Are there any updates on this issue?

beyerenrico avatar Jul 27 '22 10:07 beyerenrico