简体   繁体   English

当我尝试将它添加到材质 UI 开关时,React typescript onChange 显示错误

[英]React typescript onChange is showing error when I try to add it to material UI switch

I am planning to do a show and hide using switch.我打算使用 switch 进行显示和隐藏。 When I turn on the switch it need to show the component and when I turn odd the switch it need to hide the component.当我打开开关时,它需要显示组件,当我打开开关时,它需要隐藏组件。

This is the coding I did.这是我做的编码。

export const VirtualEventSection = ({
  control,
  onSearch,
  onSearchInputChange,
  searchInputValue,
  }: VirtualEventSectionProps) => {
  const { formatMessage } = useLocale();
  const styles = useStyles();
  const [state, setState] = useState(false);

  const handleSwitchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    setState(event.target.checked);
  };

  return (
    <Card className={styles.actionCard}>
      <Grid container spacing={1} alignItems="flex-start">
        <Grid item sm={12} xs={12}>
          <SwitchWithLabel
            name="virtualEnabled"
            label={formatMessage({ id: 'form_event.event_toggle_lable' })}
            control={control}
            checked={state}
            onChange={handleSwitchChange}
          />
          {state && state === true ? (
            <LocationSelection
              control={control}
              onSearch={onSearch}
              onSearchInputChange={onSearchInputChange}
              searchInputValue={searchInputValue}
            />
          ) : (
            <h1></h1>
          )}
        </Grid>
      </Grid>
    </Card>
  );
  };

But when I add like this I am getting an error in onChange .但是当我这样添加时,我在onChange收到错误。 This is the error I got这是我得到的错误

Type '(event: React.ChangeEvent<HTMLInputElement>) => void' is not assignable to type '({ name, checked }: OnChangeProps<"virtualEnabled">) => void'. Types of parameters 'event' and '__0' are incompatible.Type 'OnChangeProps<"virtualEnabled">' is missing the following properties from type 'ChangeEvent<HTMLInputElement>': target, nativeEvent, currentTarget, bubbles, and 11 more.

This is the created switchWithLable component这是创建的switchWithLable组件

export const SwitchWithLabel = <T extends string = string>({
  label,
  name,
  control,
  ...switchProps
}: SwitchWithLabelProps<T>) => {
const styles = useStyles();

return (
  <Controller
    name={name}
    control={control}
    render={({ value, onChange }) => (
      <FormControlLabel
        className={styles.label}
        label={label}
        control={
          <Switch
            name={name}
            onChange={async ({ checked }) => {
              onChange(checked);
            }}
            checked={value}
            {...switchProps}
          />
        }
      />
    )}
  />
);
};

Can any one help me to achieve the goal.任何人都可以帮助我实现目标。 I tried multiple time and still showing the error.我尝试了多次,仍然显示错误。

You don't need to have event: React.ChangeEvent<HTMLInputElement> .你不需要event: React.ChangeEvent<HTMLInputElement> You can change the state of the switch with simple function.您可以使用简单的功能更改开关的状态。

const handleSwitchChange = () => {
  setState(!state);
};

Complete code =>完整代码 =>

export const VirtualEventSection = ({
  control,
  onSearch,
  onSearchInputChange,
  searchInputValue,
}: VirtualEventSectionProps) => {
const { formatMessage } = useLocale();
const styles = useStyles();
const [state, setState] = useState(false);

const handleSwitchChange = () => {
  setState(!state);
};

return (
  <Card className={styles.actionCard}>
    <Grid container spacing={1} alignItems="flex-start">
      <Grid item sm={12} xs={12}>
        <SwitchWithLabel
          name="virtualEnabled"
          label={formatMessage({ id: 'form_event.event_toggle_lable' })}
          control={control}
          checked={state}
          onChange={() => handleSwitchChange()}
        />
        {state && state === true ? (
          <LocationSelection
            control={control}
            onSearch={onSearch}
            onSearchInputChange={onSearchInputChange}
            searchInputValue={searchInputValue}
          />
        ) : (
          <h1></h1>
        )}
      </Grid>
    </Grid>
  </Card>
);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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