typescript 访问父组件中的组件属性的正确/最佳方法

kmb7vmvb  于 2023-01-21  发布在  TypeScript
关注(0)|答案(2)|浏览(152)

我实际上是React的新手,我已经定制了MUI DatePicker组件,以便在不同的表单中使用

import { FormControl, FormLabel, TextField } from "@mui/material";
import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { Dayjs } from "dayjs";
import React from "react";

interface DatePickerProps {
  label: string;
  id:string;
}

const MyDatePicker = (props: DatePickerProps) => {
  const [value, setValue] = React.useState<Dayjs | null>(null);
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <FormControl sx={{ m: 1, width: "90%" }} size="small">
        <DatePicker
          label={props.label}
          value={value}
          onChange={(newValue) => {
            setValue(newValue);
          }}
          renderInput={(params) => <TextField id={props.id} {...params} size="small" />}
        />
      </FormControl>
    </LocalizationProvider>
  );
};

export default MyDatePicker;

所以在我的任何组件中,我都可以直接使用那个组件,而且它看起来工作得很好。
但我的问题是,如果在我使用该组件的同一表单上有大约10个不同的位置,我如何读取该DatePicker的值。
请帮帮忙

nafvub8i

nafvub8i1#

惯用的方法是提升组件的状态--这正是MUI通过提供值setValue props所做的。

import { FormControl, FormLabel, TextField } from "@mui/material";
import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { Dayjs } from "dayjs";
import React from "react";

interface DatePickerProps {
  label: string;
  id:string;
  value: Dayjs | null;
  setValue: (newValue: Dayjs | null) => void;
}

const MyDatePicker = (props: DatePickerProps) => {
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <FormControl sx={{ m: 1, width: "90%" }} size="small">
        <DatePicker
          label={props.label}
          value={props.value}
          onChange={props.setValue}
          renderInput={(params) => <TextField id={props.id} {...params} size="small" />}
        />
      </FormControl>
    </LocalizationProvider>
  );
};

export default MyDatePicker;

const ComponentWhereYouWantToUseMyDatePicker() => {  
  const [value, setValue] = React.useState<Dayjs | null>(null);

  // Do with value whatever you want.  

  return (
    <MyDatePicker label="Label" id="Id" value={value} setValue={setValue} />
  )
}
fnx2tebb

fnx2tebb2#

您可以添加回调onDateChange,这样就不必提升状态。

interface DatePickerProps {
  label: string;
  id: string;
  onDateChange?: (newValue: Dayjs | null) => void;
}

const MyDatePicker = (props: DatePickerProps) => {
  const [value, setValue] = React.useState<Dayjs | null>(null);
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <FormControl sx={{ m: 1, width: "90%" }} size="small">
        <DatePicker
          label={props.label}
          value={value}
          onChange={(newValue) => {
            setValue(newValue);
            if (props.onDateChange) props.onDateChange(newValue);
          }}
          renderInput={(params) => (
            <TextField id={props.id} {...params} size="small" />
          )}
        />
      </FormControl>
    </LocalizationProvider>
  );
};

您可以像这样在组件中使用它

const Component = () => {
  const handleDateChange = (newDate) => {
    console.log(newDate);
    // whatever you want to do with the value
  };

  return (
    <>
      <MyDatePicker
        id="someid"
        label="my label"
        onDateChange={handleDateChange}
      />
    </>
  );
};

相关问题