So the situation - I want to create application\framework that would utilize React and user\3rd party built components (Widgets). My current view is that I'll have a Root component that will list loaded 3rd party components. Like so:
class Root {
constructor(p){
this.state = {loadedWidgets: [<Chat/>, <Mail/>, <Notes/>]}
}
render(){
return(
<div>
<Header/>
<Body>
{this.state.loadedWidgets.map(WidgetName => <WidgetName />)}
</Body>
</div>
)
}
}
But I encountered two major problems that I can't understand or find how to solve:
- How would I load (import) those widgets if I shouldn't hard code them into Root component? (I suppose I could make some hooks so that after user install widgets it'll be added to state and page somehow)
- Why React doesn't render real components that way:
(List end for code tag to work properly)
render(){
const NameOfACustomComponent = this.state.loadedWidgets[0]
return(<div><NameOfACustomComponent/></div>
}
After rendering it'll be a custom DOM tag and not existing React Component.
What I want is to build is an extension for chrome to substitute new page with small widgets\apps that user frequently uses and at the same time leave for developers possibility to add new widgets in simple enough manner with pure React.js and some hooks for integration.
EDIT. Found answer to a 2nd question, according to this issue React allow to make arbitrary components if you have reference to its class or function (ES5 React style). So one of the solutions to both 2 and 1 questions is to have some registry in window object where any component could just add itself up and be available to the framework. Though it should be done in strict Class instance manner to provide a method to add\remove only Widget itself and not an entire list of them.