search

Home  >  Q&A  >  body text

In React, execute a function in the parent component by detecting the call of the child component

I need to call a function when a child element of the date picker is clicked

<DropdownParent >
                <DatePicker
                    selectsStart
                    showYearDropdown
                    showMonthDropdown
                    endDate={endDate}
                    selected={startDate}
                    scrollableYearDropdown
                    dateFormat="dd/MM/yyyy"
                    startDate={startDate}
                    yearDropdownItemNumber={10}
                    onChange={(date) => handleChange({ startDateObj: date })}
                    placeholderText={placeholderStart}
                    inline
                    locale="en-gb"
                />
            </DropdownDefaultAtom>

There is a toggle function in DropdownParent, I need to call it when the child function handleChange is called, I tried callbacks and other methods, but since it is a library, I don't know how to detect the change and pass it to the parent component to call the parent function.

P粉154798196P粉154798196437 days ago698

reply all(1)I'll reply

  • P粉470645222

    P粉4706452222023-09-11 18:20:04

    Update the value in Redux, then use it in the change detection of the child component after the value has been updated in Redux, and you can use it throughout the application.

    reply
    0
  • Cancelreply