How can I turn required props into optional props?

I am using Material-UI, I am trying to use the Modal component along with MobX state.

The Modal api https://material-ui.com/api/modal/ says that children and open are the only 2 required props. The problem I am facing is with the children prop since before opening the modal, there’s no children.

This is how it’s been declared on the store:

  @observable
  modalConfig: {
    open: boolean,
    body?: React.ReactElement
  } = { open: false, body: undefined };

  @action
  openDialog (body?: React.ReactElement): void {
    this.modalConfig.open = true;
    this.modalConfig.body = body;
  }

  @action
  closeDialog (): void {
    this.modalConfig.open = false;
    this.modalConfig.body = undefined;
  }

The component:

import Modal, { ModalProps } from '@material-ui/core/Modal';
import { useStores } from '../useStores';
import { useObserver } from 'mobx-react';

export const MyModal: React.FC<ModalProps> = (props) => useObserver(() => {
  const { uiStore: { modalConfig: { open, body } } } = useStores();

  const { children = body } = props;

//TS2322: Type 'Element | undefined' is not assignable to type 'ReactElement<any, any> | null'.
//Type 'undefined' is not assignable to type 'ReactElement<any, any> | null'.

// The { children } line below, throws the error above.

  return <Modal {...{ ...props, open }}>{ children }</Modal>;
});

These are the ModalProps from Material-UI that I am using. I want to give the option to another devs, to use the whole props of the modal when they need it.

export interface ModalProps
  extends StandardProps<React.HTMLAttributes<HTMLDivElement>, never, 'children'> {
  BackdropComponent?: React.ElementType<BackdropProps>;
  BackdropProps?: Partial<BackdropProps>;
  children: React.ReactElement;
  closeAfterTransition?: boolean;
  container?: PortalProps['container'];
  disableAutoFocus?: boolean;
  disableBackdropClick?: boolean;
  disableEnforceFocus?: boolean;
  disableEscapeKeyDown?: boolean;
  disablePortal?: PortalProps['disablePortal'];
  disableRestoreFocus?: boolean;
  disableScrollLock?: boolean;
  hideBackdrop?: boolean;
  keepMounted?: boolean;
  manager?: ModalManager;
  onBackdropClick?: React.ReactEventHandler<{}>;
  /**
   * Callback fired when the component requests to be closed.
   *
   * @param {object} event The event source of the callback.
   * @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`.
   */
  onClose?: {
    bivarianceHack(event: {}, reason: 'backdropClick' | 'escapeKeyDown'): void;
  }['bivarianceHack'];
  onEscapeKeyDown?: React.ReactEventHandler<{}>;
  onRendered?: PortalProps['onRendered'];
  open: boolean;
}

And this is how I call it:

//Type '{}' is missing the following properties from type 'ModalProps': children, opents(2739)

// When I call it I get the error above.
<MyModal />

What do you think? Any ideas on how to make it work?

162 thoughts on “How can I turn required props into optional props?”

  1. Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve
    really enjoyed browsing your blog posts. In any case I will
    be subscribing in your feed and I am hoping you write once more
    soon!

    Reply
  2. Hmm it seems like your blog ate my first comment (it was extremely long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog writer but I’m still new
    to everything. Do you have any tips for beginner blog writers?
    I’d certainly appreciate it.

    Reply
  3. I was curious if you ever considered changing the structure of your blog?
    Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or two pictures.
    Maybe you could space it out better?

    Reply
  4. Hi there fantastic blog! Does running a blog such as this take
    a massive amount work? I have very little expertise in computer programming but I had been hoping
    to start my own blog soon. Anyway, if you have any recommendations or tips for new
    blog owners please share. I understand this is off
    subject but I simply needed to ask. Thanks!

    Reply
  5. My squelch went to meet a medicament from our municipal zithromax 250 , ignoring them halving the instruction since yesterday they obtain not processed it and teeth of my quiet being in consummate affliction they told him to into promote after 4pm. They then told him on the phone when he rang to pinch a doggedness ‘if you privation to oneself understood a grievance in, put one in!’ Charming! They told him they be suffering with the medication in family but hadn’t unpacked it and wouldn’t do so while he waited. There were 3 other customers on top of everything else him and there were more pikestaff than customers but they were too intricate to lend a hand him! Altogether loathsome character usefulness!

    Reply
  6. After calling our scan, then my wife, I ushered substitute for azithromycin 250 into the barn & did my upper-class to spring the bleeding, and try to gap patiently for the cavalry to arrive. Our vet looked George over & determined the nature & severity of the cut was “inoperable”; interpretation that apart from administering some antibiotics to ward free infection, we had no selection but to disenchant George & Mother Nature work together on this one. This was unacceptable to us; so the next period I paid a visit to Henry at the Cloverdale Pharmasave.

    Reply