简体   繁体   English

"React Antd 无法从自定义日期选择器中获取价值"

[英]React Antd not able to get value from custom date picker

I am currently using custom datepicker to enter date in the format 'MMDD' or 'MMDDYY' and setting the value in state and using in the datepicker component as controlled component.我目前正在使用自定义日期选择器以“MMDD”或“MMDDYY”格式输入日期,并将值设置为状态并在日期选择器组件中用作受控组件。 But on submit of form the datepicker value is undefined.但是在提交表单时,日期选择器的值是未定义的。

Please find the code in the codesandbox and correct me on what i am doing wrong in this one.请在代码框中找到代码,并纠正我在此代码中做错了什么。

https:\/\/codesandbox.io\/s\/damp-haze-mmj80<\/a> https:\/\/codesandbox.io\/s\/damp-haze-mmj80<\/a>

"

Only had to add a componentDidMount cycle:只需要添加一个componentDidMount循环:

componentDidMount = () => {
  this.props.onChange(this.state.value.format('YYYY-MM-DD'));
};

handleOnChange = date => {
  if (!!date) {
    this.setState({ value: date });
    this.props.onChange(date.format('YYYY-MM-DD'));
  }
};

编辑friendly-pare-21zso

I've not used that library (antd), but looking at the docs for it, using that fieldDecorator adds two properties to the component -- onChange and value.我没有使用过那个库 (antd),但是查看了它的文档,使用那个 fieldDecorator 向组件添加了两个属性——onChange 和 value。

  {getFieldDecorator("date-picker")(<CustomDatePicker />)}

So now imagine CustomDatePicker has those two properties.所以现在想象 CustomDatePicker 有这两个属性。 Value will be the value of the form item, and onChange is expecting to be called as the onChange handler of a form input. Value 将是表单项的值,并且 onChange 期望作为表单输入的 onChange 处理程序被调用。

However, in your CustomDatePicker component you aren't doing anything with either of those.但是,在您的 CustomDatePicker 组件中,您没有对其中任何一个进行任何操作。 Instead, you're tracking the value (and updating it with a handler) locally to that component with local state.相反,您正在本地跟踪具有本地状态的组件的值(并使用处理程序更新它)。

Instead, you should use those two properties:相反,您应该使用这两个属性:

 class CustomDatePicker extends React.Component {
  state = {
    isOpen: false
  };

  render() {
    return (
      <DatePicker
        value={this.props.value}
        format={this.state.isOpen ? ["MMDD", "MMDDYY"] : "MM/DD/YY"}
        placeholder=" -- Select Date --"
        disabledDate={d => d && d > moment(Date.now())}
        onOpenChange={status => {
          this.setState({ isOpen: status });
        }}
        onChange={this.props.onChange}
      />
    );
  }
}

If you want to do any initial value logic, or validation, you'll do that at the form-level component -- the one that is controlling the state.如果您想执行任何初始值逻辑或验证,您将在表单级组件中执行此操作——控制状态的组件。

This is what works for me with React Hooks, hope it will help.这就是 React Hooks 对我有用的方法,希望它会有所帮助。

const initialInfo = {email: "", registrationDate: ""}
const [infoData, setInfoData] = useState(initialInfo);

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

相关问题 从日期选择器中获取日期值 - Get date value from date picker 我无法在 reactjs 中将自定义样式设置为 antd Date Picker - I can't set custom style to antd Date Picker in reactjs 如何在React日期范围选择器中获取开始和结束日期值? - How to get start and end date value in React date range picker? 如何从React中的DateTime选择器获取值? - How to get a value from DateTime picker in React? 每当我更改日期时,如何从 React 日期选择器中获取价值? - How do i get value from a React date picker whenever i change the dates? 如何获取 Form.Control 的价值 - React Bootstrap Date Picker? - How to get value of Form.Control - React Bootstrap Date Picker? 如何使用 API 中的数据从日期选择器中获取值 - How to get value from date picker using data from API 如何从Material Ui获取Date Picker的值 - How to get value of Date Picker from Material Ui 从jQuery日期选择器中获取查询的值(格式问题) - get value of query from jQuery date picker (format issue) 有没有办法禁用输入/粘贴到日期输入,只允许用户从 AntD 中的日期选择器中输入 select? - Is there a way to disable typing/pasting into the date input and only allowing users to select from the date picker in AntD?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM