最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - reactjs clear date input after clicking clear button - Stack Overflow

programmeradmin0浏览0评论

What should I do to clear date input?, I have button that if the user clicked, it will clear the date.

  const [startdate, setStartDate] = useState();
  const [endDate, setEndDate] = useState();


  const onChangedate = (e) => {
      let startDate = convertDate(e.startDate);
      let endDate = convertDate(e.endDate);
      setStartDate(startDate);
      setEndDate(endDate);
  }; 
  const handleClearfilter = () =>{
    setStartDate("");
    setEndDate("");
  }

  <DateRangePickerComponent   placeholder='Select a range' change={onChangedate} 
              style={{paddingTop: 10, fontSize: 16}}
              />


 <Button 
    onClick={handleClearfilter}
  >
  Clear All
  </Button>

What should I do to clear date input?, I have button that if the user clicked, it will clear the date.

  const [startdate, setStartDate] = useState();
  const [endDate, setEndDate] = useState();


  const onChangedate = (e) => {
      let startDate = convertDate(e.startDate);
      let endDate = convertDate(e.endDate);
      setStartDate(startDate);
      setEndDate(endDate);
  }; 
  const handleClearfilter = () =>{
    setStartDate("");
    setEndDate("");
  }

  <DateRangePickerComponent   placeholder='Select a range' change={onChangedate} 
              style={{paddingTop: 10, fontSize: 16}}
              />


 <Button 
    onClick={handleClearfilter}
  >
  Clear All
  </Button>

Share Improve this question asked Oct 12, 2021 at 6:55 May YieMay Yie 871 silver badge11 bronze badges 3
  • Is the issue that DateRangePickerComponent is uncontrolled and holding on to the inputted values? – Drew Reese Commented Oct 12, 2021 at 6:57
  • 1 any runnable code like codesandbox or Jsfiddle or codepen?? – Md. Abu Sayed Commented Oct 12, 2021 at 6:58
  • 1 If it is a third-party ponent please check their documentation. If you have created it yourself please handle it yourself. – Shivam Kumar Commented Oct 12, 2021 at 6:59
Add a ment  | 

2 Answers 2

Reset to default 3

I suggest using a React key to "reset" the uncontrolled input. When the React key changes React will throw away the old version and mount a new "instance".

See fully uncontrolled ponent with a key for example and deeper explanation.

const [key, setKey] = useState(false);

...

const handleClearfilter = () =>{
  setStartDate("");
  setEndDate("");
  setKey(k => !k); // toggle React key value
}

<DateRangePickerComponent
  key={key} // <-- attach React key
  placeholder='Select a range'
  change={onChangedate} 
  style={{paddingTop: 10, fontSize: 16}}
/>

If you want to control the ponent, you have to feed in the values yourself. The docs show that you require two date values since it is a data range input.

  const [startdate, setStartDate] = useState();
  const [endDate, setEndDate] = useState();


  const onChangedate = (e) => {
      let startDate = convertDate(e.startDate);
      let endDate = convertDate(e.endDate);
      setStartDate(startDate);
      setEndDate(endDate);
  }; 
  const handleClearfilter = () =>{
    setStartDate("");
    setEndDate("");
  }

  <DateRangePickerComponent   placeholder='Select a range' change={onChangedate}   startDate={startDate} endDate={endDate}
              style={{paddingTop: 10, fontSize: 16}}
              />


 <Button 
    onClick={handleClearfilter}
  >
  Clear All
  </Button>


发布评论

评论列表(0)

  1. 暂无评论