Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event is null when using isClearable on react-select

I am passing through the react-select Select component as an InputComponent within the Material-UI InputBase component. I have successfully been able to populate the value from the options, however, I'm unable to use isClearable.

When isClearable is triggered, null is passed to the handleChange(event) function and I'm hoping there is a way to force an object through to prevent null creating an error.

The handleChange function within InputBase has var element = event.target || inputRef.current. As event is null, it's not even getting to inputRef which will contain the required object.

Would be good to get this working as an uncontrolled component.

I have created a codebox to illustrate the problem: https://codesandbox.io/s/morning-feather-l7xqf

like image 573
Jetchy Avatar asked Oct 23 '25 23:10

Jetchy


1 Answers

You could supply your custom onChange() to catch the null and pass through your own value:

// Deconstruct from otherProps
const SelectWrapper = ({ inputRef, onChange, ...otherProps }) => {
  
  function handleChange(event) {
    // Overwrite the event with your own object if it doesn't exist
    if (!event) {
      event = {
        target: inputRef,
        value: '',
      };
    }
    onChange(event);
  }
  
  return (
    // Pass in the custom handleChange
    <Select styles={customStyle} isClearable ref={inputRef} onChange={handleChange} {...otherProps} />
  );
};
like image 55
Emre Koc Avatar answered Oct 26 '25 23:10

Emre Koc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!