Search code examples
javascriptcssreactjsreact-props

How to dynamically set defaultProps in React


Been trying to find a way to dynamically set the default props in React for the prop

Shop.defaultProps = {
  address: {props.address}
  status: 'open'
};

Is there anyway to set the default props dynamically?


Solution

  • React props are immutable, if you want to mutate something, then it needs to be in state. Default props are there as more of a "guard" against a null/undefined value, or if you literally want a default value/setting.

    If the prop is dynamic, it doesn't need to be "set", but rather address it later in the component via state.