Skip to content

Instantly share code, notes, and snippets.

@eddyw
Last active February 5, 2018 07:13
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 eddyw/6554b58a978b998fc332ad08cacb60f9 to your computer and use it in GitHub Desktop.
Save eddyw/6554b58a978b998fc332ad08cacb60f9 to your computer and use it in GitHub Desktop.
Type-checking Redux State with propTypes (without React)
import withPropTypes from 'withPropTypes.js'
const ItemSchema = {
id: propTypes.number,
done: propTypes.bool.isRequired,
title: propTypes.string.isRequired,
}
const TodoReducerSchema = propTypes.arrayOf(
propTypes.shape(ItemSchema).isRequired
).isRequired
let id = 0
const TodoReducer = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [...state, {
id: ++id,
title: action.payload.title,
done: action.payload.done,
}]
default:
return state
}
}
export default withPropTypes(
'TodoReducer',
TodoReducerSchema,
)(TodoReducer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment