Adding default prop on your component

Here is how to inject a variable via a prop if it exists, otherwise you can define the default value that will be returned. This doesn't only work for React components but also in a variable assignment or in a more classical javascript function.

<MyComponent age={age || 18} /> // Assign age or default 18 value for my component
const age = agePerson || 18; // Same return, just use const
Related snippets