Last active
February 5, 2018 07:13
-
-
Save eddyw/6554b58a978b998fc332ad08cacb60f9 to your computer and use it in GitHub Desktop.
Type-checking Redux State with propTypes (without React)
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
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