Skip to content

Instantly share code, notes, and snippets.

View agoldis's full-sized avatar
🎯
Focused

Andrew Goldis agoldis

🎯
Focused
View GitHub Profile
@agoldis
agoldis / deriveState.js
Created April 27, 2018 05:29
Redux - deriveState store enhancer
const deriveState = state => {
state.foo = "bar";
return state;
}
const enhanceStore = createStore => (reducer, preloadedState, enhancer) => {
const store = createStore(reducer, preloadedState, enhancer);
const _originalGetState = store.getState;
store.getState = () => deriveState(_originalGetState());
return store;
@agoldis
agoldis / spreadingHidden.jsx
Last active April 18, 2018 07:00
hidden property - spread example
const propsA = {};
Object.defineProperty(propsA, "someField", {
get: () => 42,
enumerable: false
});
const propsB = { someField: 42 };
console.log({ ...propsA }); // {}
console.log({ ...propsB }); // { someField: 42 }
console.log({ someField: propsA.someField }); // { someField: 42 }
@agoldis
agoldis / hideGetter.js
Created April 18, 2018 06:32
redux - hide getter
/* initialState.js */
const books = [ /* ... */ ]
const users = [ /* ... */ ]
const comments = [ /* ... */ ];
const usersSummary = function() {
return this.users.map(user => ({
...user,
booksCount: user.books.length,
commentsCount: this.comments.filter(c => c.user === user.id).length
@agoldis
agoldis / usersMapping.jsx
Created April 18, 2018 06:25
redux - map state to props example
/* initialState.js */
const books = [ /* ... */ ]
const users = [ /* ... */ ]
const comments = [ /* ... */ ];
const usersSummary = function() {
return this.users.map(user => ({
...user,
booksCount: user.books.length,
commentsCount: this.comments.filter(c => c.user === user.id).length
@agoldis
agoldis / stateMappers.js
Created April 18, 2018 05:35
redux - map state to props example
// Users list component mapper
const mapStateToProps = ({ users, comments }) => ({
users: users.map(user => ({
...user,
booksCount: user.books.length,
commentsCount: comments.filter(c => c.user === user.id).length
}))
});
// Books list component mapper
@agoldis
agoldis / initialState.js
Created April 11, 2018 05:12
redux blog - initialState with reselect
import { createSelector } from "reselect";
const initialState = {
app: {
users: [{ id: "user01", name: "John" }, { id: "user02", name: "Bob" }],
tasks: [
{ id: "task01", title: "Wash car", assignee: "user01" },
{ id: "task02", title: "Watch tutorial", assignee: "user01" },
{ id: "task03", title: "Do Homework", assignee: "user02" }
]
@agoldis
agoldis / initialState.js
Last active April 11, 2018 04:46
redux blog - create app initialState enhancement
const initialState = {
app: {
users: [{ id: "user01", name: "John" }, { id: "user02", name: "Bob" }],
tasks: [
{ id: "task01", title: "Wash car", assignee: "user01" },
{ id: "task02", title: "Watch tutorial", assignee: "user01" },
{ id: "task03", title: "Do Homework", assignee: "user02" }
]
},
ui: { /* ... */ }
@agoldis
agoldis / Header.jsx
Created April 11, 2018 04:12
reduxBlog - decoupled state A
import React from "react";
import { connect } from "react-redux";
const mapStateToProps = (({ userTasksCount }) => ({ userTasksCount }));
class Header extends React.Component {
render() {
const { userTasksCount } = this.props;
return <div>
{JSON.stringify(userTasksCount)}
const initialState = {
users: [{ id: "user01", name: "John" }, { id: "user02", name: "Bob" }],
tasks: [
{ id: "task01", title: "Wash car", assignee: "user01" },
{ id: "task02", title: "Watch tutorial", assignee: "user01" },
{ id: "task03", title: "Do Homework", assignee: "user02" }
],
profiles: [
{ id: "profile01", user: "user01", picture: "picture01URL" },
{ id: "profile02", user: "user02", picture: "picture02URL" }
const initialState = {
users: [{ id: "user01", name: "John" }, { id: "user02", name: "Bob" }],
tasks: [
{ id: "task01", title: "Wash car", assignee: "user01" },
{ id: "task02", title: "Watch tutorial", assignee: "user01" },
{ id: "task03", title: "Do Homework", assignee: "user02" }
],
get userTasks() {
return this.users.map(user =>
Object.assign({}, user, {