将数组传递给文本字段

Passing an array to textfield

我从后端收到了这个响应,显然它是一个对象集合,

 "deductions": [
            {
                "id": 3,
                "receiptId": 3,
                "type": "loan",
                "amount": 200,
                "reason": "You have took a loan...",
                "createdAt": "2022-02-28T13:16:38.219Z",
                "updatedAt": "2022-02-28T13:16:38.219Z",
                "deletedAt": null
            }
        ]

我在“Deduction”数组中包含三个字段,并且我使用了“TextField”,如代码中所示

但问题是“推导”是一个数组,我不知道如何传递它才能将它们全部显示在屏幕上

<tbody>
              <tr>
                <td>
                  <Controller
                    name="deductions.amount"
                    control={control}
                    render={({ field }) => (
                      <TextField
                        {...field}
                        className="mt-8 mb-16"
                        // error={!!errors.salary.amount}
                        required
                        // helperText={errors?.salary.amount?.message}
                        // label="amount"
                        autoFocus
                        id="deductions.amount"
                        variant="outlined"
                        fullWidth
                        InputProps={{
                          startAdornment: (
                            <InputAdornment position="start">£</InputAdornment>
                          ),
                        }}
                      />
                    )}
                  />
                </td>
                <td>
                  <Controller
                    name="deductions.type"
                    control={control}
                    render={({ field }) => (
                      <TextField
                        {...field}
                        className="mt-8 mb-16"
                        // error={!!errors.salary.bonus}
                        required
                        // helperText={errors?.salary.bonus?.message}
                        // label="Type"
                        autoFocus
                        id="deductions.type"
                        variant="outlined"
                        fullWidth
                      />
                    )}
                  />
                </td>
                <td>
                  <span className="truncate">
                    {" "}
                    <Controller
                      name="deductions.reason"
                      control={control}
                      render={({ field }) => (
                        <TextField
                          {...field}
                          className="mt-8 mb-16"
                          // error={!!errors.salary.workStartDate}
                          required
                          // helperText={errors?.salary.workStartDate?.message}
                          // label="Reason"
                          autoFocus
                          id="deductions.reason"
                          variant="outlined"
                          fullWidth
                        />
                      )}
                    />
                  </span>
                </td>
              </tr>
            </tbody>

我尝试使用地图,但失败了

我该如何解决这个问题?

{order.deductions.map((deduction) => (
                <tr key={deduction.id}>
                  <td>
                    <Controller
                      name="deductions.amount"
                      control={control}
                      render={({ field }) => (
                        <TextField
                          {...field}
                          className="mt-8 mb-16"
                          // error={!!errors.salary.amount}
                          required
                          // helperText={errors?.salary.amount?.message}
                          // label="amount"
                          autoFocus
                          id="deductions.amount"
                          variant="outlined"
                          fullWidth
                          InputProps={{
                            startAdornment: (
                              <InputAdornment position="start">
                                £
                              </InputAdornment>
                            ),
                          }}
                        />
                      )}
                    />
                  </td>
                  <td>
                    <Controller
                      name="deductions.type"
                      control={control}
                      render={({ field }) => (
                        <TextField
                          {...field}
                          className="mt-8 mb-16"
                          // error={!!errors.salary.bonus}
                          required
                          // helperText={errors?.salary.bonus?.message}
                          // label="Type"
                          autoFocus
                          id="deductions.type"
                          variant="outlined"
                          fullWidth
                        />
                      )}
                    />
                  </td>
                  <td>
                    <span className="truncate">
                      {" "}
                      <Controller
                        name="deductions.reason"
                        control={control}
                        render={({ field }) => (
                          <TextField
                            {...field}
                            className="mt-8 mb-16"
                            // error={!!errors.salary.workStartDate}
                            required
                            // helperText={errors?.salary.workStartDate?.message}
                            // label="Reason"
                            autoFocus
                            id="deductions.reason"
                            variant="outlined"
                            fullWidth
                          />
                        )}
                      />
                    </span>
                  </td>
                </tr>
              ))}

我希望你想将响应数据设置到输入字段,所以你可以像下面那样做

{order?.deductions.map((deduction, index) => (
                <tr key={deduction.id}>
                  <td>
                    <Controller
                      name={deduction.amount}
                      control={control}
                      render={({ field }) => (
                        <TextField
                          {...field}
                          value={deduction.amount}
                          className="mt-8 mb-16"
                          // error={!!errors.salary.amount}
                          required
                          // helperText={errors?.salary.amount?.message}
                          // label="amount"
                          autoFocus
                          id={deduction.amount}
                          variant="outlined"
                          fullWidth
                          InputProps={{
                            startAdornment: (
                              <InputAdornment position="start">
                                £
                              </InputAdornment>
                            ),
                          }}
                        />
                      )}
                    />
                  </td>
                  <td>
                    <Controller
                      name={deduction.type}
                      control={control}
                      render={({ field }) => (
                        <TextField
                          {...field}
                          value={deduction.type}
                          className="mt-8 mb-16"
                          // error={!!errors.salary.bonus}
                          required
                          // helperText={errors?.salary.bonus?.message}
                          // label="Type"
                          autoFocus
                          id={deduction.type}
                          variant="outlined"
                          fullWidth
                        />
                      )}
                    />
                  </td>
                  <td>
                    <span className="truncate">
                      {" "}
                      <Controller
                        name={deduction.reason}
                        control={control}
                        render={({ field }) => (
                          <TextField
                            {...field}
                            value={deduction.reason}
                            className="mt-8 mb-16"
                            // error={!!errors.salary.workStartDate}
                            required
                            // helperText={errors?.salary.workStartDate?.message}
                            // label="Reason"
                            autoFocus
                            id={deduction.reason}
                            variant="outlined"
                            fullWidth
                          />
                        )}
                      />
                    </span>
                  </td>
                </tr>
              ))}