2

我正在尝试使用默认检查值呈现 mui Switch 组件。但它不起作用,我收到警告。

MUI: A component is changing the default checked state of an uncontrolled SwitchBase after being initialized. To suppress this warning opt to use a controlled SwitchBase. 3 useControlled.js:27

我也尝试过设置checked属性,onChange但它仍然不起作用!我无法弄清楚我在这里做错了什么。

{details.schedule.days.map((d) => (
                <StyledTableRow>
                  <TableCell>
                    <Typography variant="subtitle1" fontWeight="bold">
                      {d.day}
                    </Typography>
                  </TableCell>
                  <TableCell>
                    <FormControlLabel
                      control={
                        <Switch
                          onChange={(e, checked) => {
                            setDetails((p) => {
                              let prev = { ...p };
                              prev.schedule?.days?.find(
                                (el) =>
                                  el.day === d.day &&
                                  (d.open = e.target.checked)
                              );
                              return prev;
                            });
                          }}

                          defaultChecked={d.open}
                        />
                      }
                      labelPlacement="bottom"
                      label={d?.open ? `Open` : `Closed`}
                    />
                  </TableCell>
                </StyledTableRow>
              ))}
4

0 回答 0