Skip to content

Instantly share code, notes, and snippets.

@akabab
Last active June 18, 2018 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akabab/709c0341451e381bc827b06288ce19c1 to your computer and use it in GitHub Desktop.
Save akabab/709c0341451e381bc827b06288ce19c1 to your computer and use it in GitHub Desktop.
Redux setup
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>
)
}
}
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
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