Last active
June 18, 2018 10:08
-
-
Save akabab/709c0341451e381bc827b06288ce19c1 to your computer and use it in GitHub Desktop.
Redux setup
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 } from 'react' | |
import store from './store.js' | |
class App extends Component { | |
componentDidMount() { | |
// subscribe to state changes -> forceUpdate will ask React to re-render the view | |
this.unsubscribe = store.subscribe(() => this.forceUpdate()) | |
} | |
componentWillUnmount() { | |
// when component is removed from the view -> remove the subscription | |
this.unsubscribe() | |
} | |
render() { | |
const state = store.getState() | |
return ( | |
<div className="App"> | |
</div> | |
) | |
} | |
} |
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 initialState = {} | |
// set initialState with default parameter value (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) | |
const reducer = (state = initialState, action) => { | |
switch (action.type) { | |
default: return state // ! always return a state object | |
} | |
} | |
export default reducer |
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 { createStore } from 'redux' | |
import reducer from './reducer.js' | |
const store = createStore(reducer) | |
export default store |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment