🆕 Default Props #79
Replies: 3 comments 5 replies
-
My view would be that approach 2 can definitely be removed in favour of either approach 1 or approach 3. Where there are only 1 or 2 default parameters, approach 1 is a bit cleaner but happy to go with approach 3 everywhere for consistency. |
Beta Was this translation helpful? Give feedback.
-
I came across a new issue today 🙃 In a number of cases, we are executing some other logic in the constructor as well as providing some default props in the Consider the following scenario where we set a default in the super call but then use that prop in the constructor too class MyClass extends ParentClass {
constructor(scope: GuStack, id: string, props: MyClassProps) {
super(scope, id, {someBooleanProp: true, ...props})
if (props.someBooleanProps) // do a thing
}
} Instead should we be doing something more like the following or is there another, better way? class MyClass extends ParentClass {
constructor(scope: GuStack, id: string, props: MyClassProps) {
const mergedProps = {someBooleanProp: true, ...props}
super(scope, id, mergedProps)
if (mergedProps.someBooleanProps) // do a thing
}
} |
Beta Was this translation helpful? Give feedback.
-
Here's a Playground with another approach that works quite well. Might need a little adjusting to cater for the scenarios that depend on the |
Beta Was this translation helpful? Give feedback.
-
Many of the constructs defined in this library set default props to simplify the process of using them. Currently there are three different approaches to doing that.
Approach 1
Some constructs use the spread operator to merge default props, passed in props and override props in the call to
super
in their constructor. For example:Approach 2
Some constructs define a static
defaultProps
object which is spread with the passed in props. For example:Approach 3
Can we reduce this to one approach everywhere?
Beta Was this translation helpful? Give feedback.
All reactions