Hacker0x01 / react-datepicker

A simple and reusable datepicker component for React
https://reactdatepicker.com/
MIT License
8.09k stars 2.24k forks source link

Invalid hook call in renderCustomHeader #3814

Open potapovnikita opened 1 year ago

potapovnikita commented 1 year ago

Before you start - checklist

Description

Invalid hook call in renderCustomHeader prop.

When I'm passing ReactComponent (with own useState hook inside) in renderCustomHeader prop, i'm getting error Invalid hook call......

Example:

<DatePicker
          renderCustomHeader={DatePickerHeader}
          selected={value}
          onChange={(date) => setValue(date)}
          inline
          locale={LOCALE}
/>

where DatePickerHeader is React Component:

const DatePickerHeader: FC<ReactDatePickerCustomHeaderProps> = () => {
  const [someState, setSomeState] = useState(false);
  return (
    <div>Some Header</div>  
  );
}

Steps to reproduce

  1. Create React Component with inner state (useState) and pass it in renderCustomHeader (as in bug description).
  2. See error in console.

Expected behavior

Should working without error Invalid hook call

Actual behavior

There is error in console Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: ......

Additional information

No response

Environment

talatkuyuk commented 1 year ago

The same issue I have.

Could you make it possible to provide custom header as a normal JSX component like the same way with Custom Input. Then maybe the internal hooks work in the custom header component.

<DatePicker
  // the rest props are omitted
  renderCustomHeader={<DatePickerHeader />}
/>
github-actions[bot] commented 4 months ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.

talatkuyuk commented 4 months ago

Still same issue.

anmatika commented 2 months ago

Extending @talatkuyuk's answer

This works for me when the props are passed. Now hook usage is possible in the header component.

<DatePicker
  // the rest props are omitted
   renderCustomHeader={(p) => <DatePickerHeader {...p} />}
/>