Skip to content

Instantly share code, notes, and snippets.

@vincsb
Last active June 21, 2019 09:19
Show Gist options
  • Save vincsb/25ffddbda963954674a311c26e587d15 to your computer and use it in GitHub Desktop.
Save vincsb/25ffddbda963954674a311c26e587d15 to your computer and use it in GitHub Desktop.
Use @lodash/fp to immutable state change in Redux reducer
import { flow, set } from 'lodash/fp';
const reducer = (state, action) => {
case HIDDEN_POST: {
flow(
set(`posts.${postId}.isHidden`, true ),
set(`posts.${postId}.commentsCount`, 0 ),
)(state);
}
}
const reducer = (state, action) => {
case HIDDEN_POST: {
if (state.posts[action.payload.postId]) {
return {
...state,
posts: {
...state.posts,
[action.payload.postId]: {
...state.posts[action.payload.postId],
isHidden: true,
commentsCount: 0,
},
},
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment