Created
July 2, 2018 15:50
-
-
Save mbbertino/17351fadc4d864f4f8c884e9fdd48cd2 to your computer and use it in GitHub Desktop.
React default state setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Current structure | |
constructor(props) { | |
super(props) | |
this.state = { | |
isValidating: false, | |
processing: false, | |
// ... other state setting from custom functions | |
// ... other state setting from props | |
} | |
} | |
// First pass | |
defaultState = { | |
isValidating: false, | |
processing: false, | |
} | |
constructor(props) { | |
super(props) | |
this.state = { | |
...this.defaultState | |
// ... | |
} | |
} | |
// I only have one issue with this, and that's the fact that we can't make it a static class property like below. | |
static defaultState = { | |
//... | |
} | |
// The reason this isn't possible is b/c static properties aren't available in the constructor since we're in the context of an instance of the class | |
// This causes us to have configuration in a couple different places which could be a concern. Like | |
static defaultProps = { | |
feeCents: 0, | |
interval: 'month', | |
intervalCount: 1, | |
subtotalCents: 0, | |
stripePublishableKey: 'no_key', | |
upfrontAmountCents: 0, | |
} | |
// and | |
defaultState = { | |
isValidating: false, | |
processing: false, | |
} | |
// Again not a super big deal but not ideal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment