Created
August 21, 2016 23:25
-
-
Save jdmorlan/29c7d41c93678ae92ba81c875d5f8016 to your computer and use it in GitHub Desktop.
Use Redux in Rails
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, combineReducers } from 'redux' | |
import { reducer as education } from 'reducers/education' | |
import { reducer as skills } from 'reducers/skills' | |
const buildResumeBuilderStore = () => { | |
return createStore(combineReducers({ education, skills }) | |
} | |
document.addEventListener('DOMContentLoaded', () => { | |
registerStoreComponent(ResumeBuilderContainer, 'resume-builder-container', buildResumeBuilderStore) | |
}) |
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 from 'react' | |
import ReactDOM from 'react-dom' | |
import { Provider } from 'react-redux' | |
const getProps = (container) => { | |
const propsString = container.getAttribute("data-react-props") | |
const props = propsString ? JSON.parse(propsString) : null | |
return props | |
} | |
export const registerStoreComponent = (Instance, containerName, getStore) => { | |
const selector = `[data-react-container=${containerName}]` | |
const container = document.querySelector(selector) | |
if (container) { | |
const props = getProps(container) | |
const store = getStore() | |
ReactDOM.render( | |
<Provider store={ store }> | |
<Instance { ...props } /> | |
</Provider>, container) | |
} | |
} |
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 from 'react' | |
import { connect } from 'react-redux' | |
import ResumeBuilder from 'components/resumeBuilder' | |
const ResumeBuilderContainer = React.createClass({ | |
render () { | |
return( | |
<ResumeBuilder /> | |
) | |
} | |
}) | |
export default connect()(ResumeBuilderContainer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment