Skip to content

Instantly share code, notes, and snippets.

@brennancheung
Created December 16, 2016 01:19
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 brennancheung/921b7c15f0917d3b7321cfa0c5a10ab7 to your computer and use it in GitHub Desktop.
Save brennancheung/921b7c15f0917d3b7321cfa0c5a10ab7 to your computer and use it in GitHub Desktop.
import React from 'react'
import ReactDOM from 'react-dom'
import Rx from 'rxjs'
import { AppContainer } from 'react-hot-loader'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import rootReducer from './reducers'
import App from './components/App'
const store = createStore(rootReducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())
const render = (state) => {
console.log('Rendering app with state=', state)
ReactDOM.render(
<AppContainer>
<Provider store={store}>
<App {...state} />
</Provider>
</AppContainer>,
document.getElementById('root')
)
}
const initialState = {
counter: 1
}
const reducer = (state, action) => {
const { type, payload } = action
if (type === 'INC_COUNTER') {
return {...state, counter: state.counter + 1}
} else {
return state
}
}
export const actions = {
INC_COUNTER: () => ({ type: 'INC_COUNTER' }),
NOOP: () => ({ type: 'NOOP' })
}
const tickFn = () => rootSubject.next(actions.INC_COUNTER())
const rootSubject = new Rx.Subject()
const subscriber = {
next: (state) => {
render(state)
}
}
rootSubject
.startWith(initialState)
.scan(reducer)
.subscribe(subscriber)
setInterval(tickFn, 1000)
render(initialState)
if (module.hot) {
module.hot.accept('./components/App', render)
rootSubject.next(actions.NOOP())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment