Skip to content

Instantly share code, notes, and snippets.

View Jahans3's full-sized avatar
:shipit:

Josh J Jahans3

:shipit:
View GitHub Profile
@Jahans3
Jahans3 / store-with-dispatch.hs
Last active November 30, 2018 03:25
Dispatch to reducers
export class Provider extends React.PureComponent {
static defaultProps = {
state: {},
reducers: []
};
state = this.props.state;
_dispatch = action => {
const { reducers } = this.props;
@Jahans3
Jahans3 / placeholder-dispatch.js
Created November 30, 2018 03:13
Dispatch placeholder
export class Provider extends React.PureComponent {
static defaultProps = {
state: {}
};
state = this.props.state;
_dispatch = action => {};
render () {
@Jahans3
Jahans3 / action-creators.js
Last active November 30, 2018 03:32
Action creators
// Action types
const ADD_ONE = 'ADD_ONE';
const ADD_N = 'ADD_N';
// Actions
export const addOne = () => ({ type: ADD_ONE });
export const addN = amount => ({ type: ADD_N, payload: amount });
@Jahans3
Jahans3 / consume-update.js
Last active December 10, 2018 17:41
Updating and consuming our state
export default function SomeCount () {
return (
<StateConsumer>
{({ state, setState }) => (
<>
<p>
Count: {state.count}
</p>
<button onClick={() => setState({ count: state.count + 1 })}>
+ 1
@Jahans3
Jahans3 / update-state.js
Last active December 16, 2018 22:02
Updating our state
export class StateProvider extends Component {
static defaultProps = {
state: {}
};
state = this.props.state;
render () {
return (
<Provider value={{ state: this.state, setState: this.setState.bind(this) }}>
@Jahans3
Jahans3 / consume-state.js
Created November 29, 2018 19:42
Consuming State
import { StateConsumer } from './stateContext';
export default function SomeCount () {
return (
<StateConsumer>
{state => (
<p>
Count: {state.count}
</p>
)}
@Jahans3
Jahans3 / using-state-provider.js
Created November 29, 2018 19:35
Using StateProvider
import { StateProvider } from './stateContext';
import MyApp from './MyApp';
const initialState = {
count: 0
};
export default function Root () {
return (
<StateProvider state={initialState}>
@Jahans3
Jahans3 / state-context-provider.js
Last active November 29, 2018 19:40
Our context's provider
import React, { Component, createContext } from 'react';
const { Provider, Consumer } = createContext();
export const StateConsumer = Consumer;
export class StateProvider extends Component {
static defaultProps = {
state: {}
};
@Jahans3
Jahans3 / create-state-context.js
Last active November 29, 2018 19:10
Creating a context
import { createContext } from 'react';
const { Provider, Consumer } = createContext();
@Jahans3
Jahans3 / super-basic-state.js
Last active November 29, 2018 18:31
Super Basic State
const state = {};
export const getState = () => state;
export const setState = nextState => {
state = nextState;
};