Created
February 9, 2016 19:48
-
-
Save samsch/63a54e868d7fa2b6023a to your computer and use it in GitHub Desktop.
Possible solution to Redux action type name confilcts.
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
//Action type constants don't have to have the same name as their value. | |
//Really, the const string value doesn't even have to be related to the action itself. | |
const UPDATE = 'randomValueGeneratedSomehow'; | |
let updateThing = function(newValue) { | |
return { | |
type: UPDATE, | |
payload: newValue, | |
} | |
} | |
module.exports = { | |
UPDATE, | |
updateThing, | |
} |
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
//The reducer doesn't care about the action type string value, only that the constant matches. | |
//So this constant name can be used by other reducers in other files without conflicting with this one. | |
let { UPDATE } = require('./actions.js'); | |
function thingReducer(state = {}, action) { | |
if(action.type === UPDATE) { | |
return action.payload; | |
} | |
return state; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment