Skip to content

Instantly share code, notes, and snippets.

@mbbertino
Created July 2, 2018 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbertino/17351fadc4d864f4f8c884e9fdd48cd2 to your computer and use it in GitHub Desktop.
Save mbbertino/17351fadc4d864f4f8c884e9fdd48cd2 to your computer and use it in GitHub Desktop.
React default state setup
// 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