Search code examples
javascriptreactjstypescriptdefaultreact-props

how to make proper default props with single objects


I have this code:

  const setup = (props: SchemaModalProps = { isOpen: false, onClose: () => { }, row: {}, onSchemaChange: () => { }, updateSchema: () => { }, hasPermission: false }) => {
    const wrapper: any = mount(<SchemaModal {...props} />);
    const driver = new SchemaModalDriver(wrapper);
    return driver;
  };

and when I call the setup function I need to specify the inner object items like so:

 const driver = setup({ isOpen: true, row: someTriggerConfiguration, onClose: () => { }, onSchemaChange: () => { }, updateSchema: () => { }, hasPermission: true });

how can I rewrite the code in such a way that if I do setup({isOpen:false}) it will only overwrite the isOpen and not the rest of them ( use their default values).


Solution

  • Based on previous questions I found this to work best:

      const setup = ({ isOpen = false, onClose = () => { }, row = {}, onInfoChange = () => { }, hasPermission = false }) => {
        const props: TriggerInfoModalProps = { isOpen, onClose, row, onInfoChange, hasPermission }
        const wrapper: any = mount(<TriggerInfoModal {...props} />);
        const driver = new TriggerInfoModalDriver(wrapper);
        return driver;
      };