Last active
January 28, 2019 21:59
-
-
Save cmstead/f5014dc3b974e1b61c4aa901515fc3f1 to your computer and use it in GitHub Desktop.
Proof of concept: Building a react app using Dject as an IoC container
This file contains 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 logo from './logo.svg'; | |
import './App.css'; | |
function AppFactory(React) { | |
const { Component } = React; | |
class App extends Component { | |
render() { | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
<img src={logo} className="App-logo" alt="logo" /> | |
<p> | |
Edit <code>src/App.js</code> and save to reload. | |
</p> | |
<a | |
className="App-link" | |
href="https://reactjs.org" | |
target="_blank" | |
rel="noopener noreferrer" | |
> | |
Learn React | |
</a> | |
</header> | |
</div> | |
); | |
} | |
} | |
return App; | |
} | |
export default { | |
moduleValue: AppFactory, | |
moduleName: 'App' | |
}; |
This file contains 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'; | |
global.dject = require('dject'); | |
describe('Base application', function () { | |
let App; | |
beforeEach(function (done) { | |
import('../ioc-registrations').then(function (result){ | |
const testContainer = result.default.new(); | |
App = testContainer.build('App'); | |
done(); | |
}); | |
}); | |
it('renders without crashing', () => { | |
const div = document.createElement('div'); | |
ReactDOM.render(<App />, div); | |
ReactDOM.unmountComponentAtNode(div); | |
}); | |
}); | |
This file contains 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
if (typeof window.window.container === 'undefined') { | |
window.container = window.dject.new({}); | |
window.container.registerImport = function ({moduleValue, moduleName}) { | |
window.container.register(moduleValue, moduleName); | |
}; | |
} | |
export default window.container; |
This file contains 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 './index.css'; | |
import * as serviceWorker from './serviceWorker'; | |
import container from './ioc-registrations'; | |
const App = container.build('App'); | |
ReactDOM.render(<App />, document.getElementById('root')); | |
// If you want your app to work offline and load faster, you can change | |
// unregister() to register() below. Note this comes with some pitfalls. | |
// Learn more about service workers: http://bit.ly/CRA-PWA | |
serviceWorker.unregister(); |
This file contains 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 container from './container'; | |
import reactRegistration from './wrappedModules/react'; | |
import appRegistration from './App'; | |
container.registerImport(appRegistration); | |
container.registerImport(reactRegistration); | |
export default container; |
This file contains 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'; | |
export default { | |
moduleValue: () => React, | |
moduleName: 'React' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment