Last active
July 19, 2018 16:05
-
-
Save merlinstardust/e76a3e4df8eedba812426a2912c426af to your computer and use it in GitHub Desktop.
A possible way to use Meteor packages with React context and dynamic imports
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
const packages = { | |
Accounts: 'accounts-base', | |
Meteor: 'meteor', | |
}; | |
const imports = Object.keys(packages).reduce((object, name) => { | |
const path = packages[name]; | |
return { | |
...object, | |
[name]: (fn, ...args) => import(`meteor/${path}`).then(source => { | |
context[name] = source.default ? source.default[name] : source[name]; | |
}), | |
} | |
}, {}); | |
const context = { | |
imports, | |
}; | |
export default context; |
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 MeteorPackages from './MeteorPackages'; | |
import context from './context'; | |
const Layout = ({content}) => { | |
return ( | |
<div className={`Layout ${themeClass}`}> | |
<MeteorPackages.Provider value={context}> | |
{content} | |
</MeteorPackages.Provider> | |
</div> | |
); | |
}; | |
export default Layout; |
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'; | |
const MeteorPackages = React.createContext(); | |
const withMeteorPackages = Component => ( | |
props => ( | |
<MeteorPackages.Consumer> | |
{meteorPackages => | |
<Component {...props} meteorPackages={meteorPackages} /> | |
} | |
</MeteorPackages.Consumer> | |
) | |
); | |
export {withMeteorPackages}; | |
export default MeteorPackages; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment