Skip to content

Instantly share code, notes, and snippets.

View smashercosmo's full-sized avatar

Vladislav Shkodin smashercosmo

View GitHub Profile
const store = createStore(
reducer,
applyMiddleware(thunk.withExtraArgument(analytics))
)
// later
function fetchUser(id) {
return (dispatch, getState, analytics) => {
analytics.track(...);
}
const store = createStore(
reducer,
applyMiddleware(thunk.withExtraArgument({analytics, api}))
)
// later
function fetchUser(id) {
return (dispatch, getState, {analytics, api}) => {
api.fetchUser().then(user => {
analytics.track(...);
export function fetchUser(id) {
return (dispatch, getState, {analytics, api}) => {
return api.fetchUser().then(user => {
analytics.track(...);
})
}
}
const store = createStore(
reducer,
applyMiddleware(thunk.withExtraArgument({analytics: () => window.analytics}))
)
// later
function fetchUser(id) {
return (dispatch, getState, {analytics}}) => {
analytics().track(...);
}
const injectMiddleware = (staticDeps, dynamicDeps) => ({ dispatch, getState }) => next => action => {
const deps = {...staticDeps};
Object.keys(dynamicDeps).forEach(key => {
deps[key] = dynamicDeps[key]();
});
return next(typeof action === 'function'
? action({ ...deps, dispatch, getState })
: action
);
}
@smashercosmo
smashercosmo / hoc.js
Last active February 14, 2018 13:33
bla
import React from 'react'
import { noop } from 'lodash'
import hoistStatics from 'hoist-non-react-statics'
import { getServerErrorMessage } from '../../lib/serverErrorUtils'
import PropTypes from "prop-types"
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
}
@smashercosmo
smashercosmo / Text.js
Created June 4, 2018 14:29
shouldForwardProp example
import styled from 'react-emotion'
import { bool } from 'prop-types'
import isPropValid from '@emotion/is-prop-valid'
import {
fontSize,
color,
textAlign,
fontWeight,
hover,
propTypes,