0

I am rendering a dynamic table in react like this:

    <TableBody>
      {finalLoadedTokensData.map((loadedToken, index) => {
        const isItemSelected = isSelected(loadedToken.contract);
        const labelId = `enhanced-table-checkbox-${index}`;

        return(
          <TableRow
            hover
            key={loadedToken.id}
            selected={isItemSelected}
            sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
          >
            <TableCell padding="checkbox">
              <Checkbox
                color="primary"
                checked={isItemSelected}
                inputProps={{
                  'aria-labelledby': labelId,
                }}
                disabled={false}
                onClick={(event) => handleClick(event, loadedToken.contract, loadedToken.type, parseInt(loadedToken.tokenId) || "-", loadedToken.quantity)}
              />
            </TableCell>
            <TableCell align="center">{loadedToken.type}</TableCell>
            <TableCell align="center">{loadedToken.contract}</TableCell>
            <TableCell align="center">{loadedToken.tokenId}</TableCell>
            <TableCell align="center">{loadedToken.quantity}</TableCell>
            <TableCell align="center">{loadedToken.lockedQuantity}</TableCell>
            <TableCell>
              <DateTimePicker
                minDate={moment().toDate()}
                value={dateValue}
              />
            </TableCell>
            <TableCell align="center">
              <Button 
                disabled={!isItemSelected} 
                color="primary" 
                variant="contained" 
                onClick={async () => {                        
                  await lockToken(selected, props.smartNftId, 1679823611)
                }}
              >
                Lock
              </Button>
            </TableCell>
          </TableRow>
        )})}
    </TableBody>

If I create a variable like the one I have (dateValue) and I handle its state with a useState, I am only managing one variable for all the possible rows I have. What I need is to be able to create a value for the DateTimePicker component but for each one of the rows.

How can I do that?

1 Answer 1

1

You can try to store the dateValue values in an array:

const [dateValues, setDateValues] = useState([])

And then in the table:

    <TableBody>
      {finalLoadedTokensData.map((loadedToken, index) => {
        const isItemSelected = isSelected(loadedToken.contract);
        const labelId = `enhanced-table-checkbox-${index}`;

        return(
          <TableRow
            key={loadedToken.id}
            selected={isItemSelected}
          >
        
            <TableCell>
              <DateTimePicker
                minDate={moment().toDate()}
                value={dateValues?.[index] || null}
                setValue={value => {
                  let newValues = [...dateValues];
                  newValues[index] = value;
                  setDateValues(newValues);
                }}
              />
            </TableCell>
          </TableRow>
        )})}
    </TableBody>
Sign up to request clarification or add additional context in comments.

4 Comments

And if I want to update the variables onChange, should I use the setter for the array there?
Yes. I'll update the answer.
Sorry but it does not work, I am getting undefined when I console.log the value of the DateTimePicker component.
There was a typo and an error in the setter. Updated the answer. Working codesandbox here: codesandbox.io/s/hopeful-dhawan-dn4d9p?file=/src/App.js:595-719

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.