I have a prop driven
that takes its value from another prop source
. driven
can be passed a value itself, breaking that link, but by default it follows it.
Setting a default prop for source
is straightforward, but how can I reference source
in driven
‘s default prop.
const Example = ({source = 'foo', driven = source}) => <div />
Example.defaultProps = {
source: 'foo',
driven: /* ? */
}
2
Answers
If you need
driven
to be an alias forsource
, you can use a nullish-coalescing operation inside the component memoization:You can use the the function for default prop value of
driven
. I am the fan of typescript so just change your code in typescript.