Created
June 28, 2017 20:39
-
-
Save RebootJeff/c298c4f80018b8af3a75826af291a52f to your computer and use it in GitHub Desktop.
Illustrating a question on how to initialize state in redux
This file contains 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
// How do I initialize state when I do *NOT* want to use hard-coded values? | |
// in MyComponent.js ... | |
const mapStateToProps = (state, ownProps) => { | |
return { | |
usernames: state.MyComponent.usernames, | |
selectedDesserts: state.MyComponent.selectedDesserts || ownProps.availableDesserts | |
}; | |
}; | |
// in MyComponent.ducks.js ... | |
const initialState = { | |
usernames: [], | |
selectedDesserts: null | |
}; | |
// Then the reducer gets a bit tricky because it has to deal with | |
// possibility of `selectedDesserts` being either `null` or an | |
// array that needs to be updated. | |
// So this feels less than ideal, but how do we initialize `selectedDesserts` | |
// to be "every available dessert" when we don't know the list of available | |
// desserts ahead of time? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment