This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Provider extends React.PureComponent { | |
static defaultProps = { | |
state: {}, | |
reducers: [] | |
}; | |
state = this.props.state; | |
_dispatch = action => { | |
const { reducers } = this.props; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Provider extends React.PureComponent { | |
static defaultProps = { | |
state: {} | |
}; | |
state = this.props.state; | |
_dispatch = action => {}; | |
render () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function SomeCount () { | |
return ( | |
<StateConsumer> | |
{({ state, setState }) => ( | |
<> | |
<p> | |
Count: {state.count} | |
</p> | |
<button onClick={() => setState({ count: state.count + 1 })}> | |
+ 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class StateProvider extends Component { | |
static defaultProps = { | |
state: {} | |
}; | |
state = this.props.state; | |
render () { | |
return ( | |
<Provider value={{ state: this.state, setState: this.setState.bind(this) }}> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { StateConsumer } from './stateContext'; | |
export default function SomeCount () { | |
return ( | |
<StateConsumer> | |
{state => ( | |
<p> | |
Count: {state.count} | |
</p> | |
)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { StateProvider } from './stateContext'; | |
import MyApp from './MyApp'; | |
const initialState = { | |
count: 0 | |
}; | |
export default function Root () { | |
return ( | |
<StateProvider state={initialState}> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component, createContext } from 'react'; | |
const { Provider, Consumer } = createContext(); | |
export const StateConsumer = Consumer; | |
export class StateProvider extends Component { | |
static defaultProps = { | |
state: {} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createContext } from 'react'; | |
const { Provider, Consumer } = createContext(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const state = {}; | |
export const getState = () => state; | |
export const setState = nextState => { | |
state = nextState; | |
}; |