What should be the standard return argument of the onChange?

Viewed 391

What should be the standard return argument of the onChange method for Selector, Checkbox... which have the list of options object{value, label} as the input?

you guys prefer an object{value, label} or a single value? It looks like this:

onChange: (event, option: object{label, value}) => {};

or

onChange: (event, value) => {};

Update: Sorry, my question is not clear and I'll add more context to it.

My project has a components library and I'm creating some components like Selector, Checkbox... which have the list of options object{value, label} as the input:

type optionProps = {
  value: string;
  label?: string;
};

export interface SelectorProps {
  options: Array<optionsProps>;
  values?: string[];
  onChange?: (event: Event, options: optionsProps) => void;
}

Notice the onChange function, some ppl told me the onChange function only needs to return the value (not a whole option) as the 2nd argument. And I'm not sure what is more appropriate. Thanks.

1 Answers
  1. In case of any change which has been caused by event i prefer to have a single value, or we prefer to access the values/properties associated with event.

  2. Basically do not returned anything from the change method as we prefer to do the required operation based on the event. And so by default it returns undefined.

  3. Few scenarios could be there where you may have to delegate the event at next level. If you want the parent to perform the operation.

Thanks

Related