Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RebootJeff/c298c4f80018b8af3a75826af291a52f to your computer and use it in GitHub Desktop.
Save RebootJeff/c298c4f80018b8af3a75826af291a52f to your computer and use it in GitHub Desktop.
Illustrating a question on how to initialize state in redux
// 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