Last active
June 12, 2022 19:22
-
-
Save davidgljay/5d7a29c5add8b360b93db838235e80a8 to your computer and use it in GitHub Desktop.
Pattern for dynamically loading React components based on a config json object.
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 config from '../config' | |
let components = {} | |
//For each component in the config fiel into an object | |
for (var i = config.length - 1; i >= 0; i--) { | |
components[config[i].name] = require(config[i].path).default | |
} | |
export default components | |
/* Alternative if this require business makes you too queasy */ | |
// import First from './First' | |
// import Second from './Second' | |
// import Third from './Third' | |
// export const First = First | |
// export const Second = Second | |
// export const Third = Third |
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
export default [ | |
{ | |
name:'First', | |
path:'./First' | |
}, | |
{ | |
name:'Second', | |
path:'./Second' | |
}, | |
{ | |
name:'Third', | |
path:'./Third' | |
}] |
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'; | |
//Example components, keeping these super simple | |
class First extends React.Component { | |
render() { | |
return <h1>First</h1> | |
} | |
} | |
export default First |
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 Components from './ComponentIndex' | |
import config from '../config' | |
class AppComponent extends React.Component { | |
render() { | |
return ( | |
<div className="index"> | |
<h2>Component structure test</h2> | |
{config.map((comp) => { | |
/*Load each component in the config file in order. | |
* Note that the config object could also include props | |
* which could be passed in like so: | |
* <Component {..config.props} /> | |
*/ | |
if (Components.hasOwnProperty(comp.name)) { | |
let Component = Components[comp.name]; | |
return <Component key={comp.name} /> | |
} | |
})} | |
</div> | |
); | |
} | |
} | |
export default AppComponent; |
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'; | |
class Second extends React.Component { | |
render() { | |
return <h1>Second</h1> | |
} | |
} | |
export default Second |
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'; | |
class Third extends React.Component { | |
render() { | |
return <h1>Third</h1> | |
} | |
} | |
export default Third |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello there,
Thank you for sharing. does it work for share
props
to childFirst.js
withcomponentWillReceiveProps
?Thank you