Skip to content

Instantly share code, notes, and snippets.

View Jahans3's full-sized avatar
:shipit:

Josh J Jahans3

:shipit:
View GitHub Profile
return (
<StateConsumer mapState={state => ({ greeting: state.greeting })} mapDispatch={dispatch => dispatch}>
// ...
@Jahans3
Jahans3 / use-state-provider.js
Last active December 1, 2018 20:11
Use state provider
export function useStateProvider ({ initialState, reducers, middleware = [] }) {
const [state, _dispatch] = useReducer((state, action) => {
return reducers.reduce((state, reducer) => reducer(state, action) || state, state);
}, initialState);
function dispatch (action) {
if (typeof action === 'function') {
return action(dispatch, state);
}
@Jahans3
Jahans3 / provider-with-middleware.js
Last active December 1, 2018 19:55
Provider with middleware
export class StateProvider extends React.PureComponent {
static defaultProps = {
state: {},
reducers: [],
middleware: []
};
state = this.props.state;
_dispatch = action => {
@Jahans3
Jahans3 / login-async.js
Created December 1, 2018 19:47
Login async
const logIn = (email, password) => async dispatch => {
dispatch({ type: 'LOG_IN_REQUEST' });
try {
const user = api.authenticateUser(email, password);
dispatch({ type: 'LOG_IN_SUCCESS', payload: user });
catch (error) {
dispatch({ type: 'LOG_IN_ERROR', payload: error });
}
};
@Jahans3
Jahans3 / use-reducer.js
Last active December 1, 2018 17:48
Use reducer
export function StateProvider ({ state: initialState, reducers, middleware, children }) {
const [state, dispatch] = useReducer((state, action) => {
return reducers.reduce((state, reducer) => reducer(state, action) || state, state);
}, initialState);
return (
<StateContext.Provider value={{ state, dispatch }}>
{children}
</StateContext.Provider>
);
@Jahans3
Jahans3 / count-reducer.js
Last active December 1, 2018 17:43
Count reducer
function countReducer ({ count, ...state }, { type, payload }) {
switch (type) {
case ADD_N:
return { ...state, count: count + payload };
case ADD_ONE:
return { ...state, count: count + 1 };
}
}
@Jahans3
Jahans3 / store-with-dispatch.js
Last active December 1, 2018 17:42
Store with dispatch
export class Provider extends React.PureComponent {
static defaultProps = {
state: {},
reducers: []
};
state = this.props.state;
_dispatch = action => {
const { reducers } = this.props;
@Jahans3
Jahans3 / final-provider-usehooks.js
Created December 1, 2018 01:59
Final provider with hooks
export function StateProvider ({ state: initialState, reducers, middleware, children }) {
return (
<StateContext.Provider value={useStateProvider({ initialState, reducers, middleware })}>
{children}
</StateContext.Provider>
);
}
@Jahans3
Jahans3 / dispatch-using-hooks.js
Created December 1, 2018 01:41
Dispatch using hooks
export default function SomeCount () {
const [state, dispatch] = useStore();
return (
<>
<p>
Count: {state.count}
</p>
<button onClick={() => dispatch(addOne())}>
+ 1
</button>
const StateContent = createContext();
export function useStore () {
const { state, dispatch } = useContext(StateContext);
return [state, dispatch];
}