Closed tomerla closed 7 years ago
In React the hosted module can do this on its own:
window.ModuleRegistry.registerComponent('appName.mainComponentName', () => withData(MainComponent));
function withData(WrappedComponent) {
class PrepareApp extends Component {
constructor(props) {
super(props);
this.state = {data: null};
}
componentDidMount() {
fetchData().then(data => {
this.setState({data});
});
}
render() {
return this.state.data ?
React.createElement(WrappedComponent, {data}) :
null;
}
}
}
@odedcagan yes, it would definitely do the trick
although I still prefer the resolve
pattern since we can fetchData()
in parallel with the browser downloading the bundles
@tomerla oh I missed that, fetching data in parallel sounds very interesting. A few things though:
type TResolve = () => Promise<object>
) instead of an object with functions, and then the code in ReactLazyComponent can simply wait for the resolve and fetch promises and do the same thing with the props.
Usage example:
{
files: [],
component: '',
resolve: () => {
return Promise.all([Promise.resolve({id: '123'}), Promise.resolve({'spec.enabled': 'true'})])
.then(([data, experiments]) => ({data, experiments}))
}
}
@shahata wdyt?
@odedcagan @tomerla I like the idea, but plz make sure api makes sense and code is reused correctly between angular and react lazy loaders. I'd like to review when you two are happy and ready to merge.
@odedcagan PR is ready, please review
I decided to use your suggestion (so resolve
will be a function of type TResolve = () => Promise<object>
instead of an object)
Please notice that there 2 commits for this PR
resolve
to the manifestresolve
logic inside a BaseLazyComponent
I think it would be easier to review them separately...
@tomerla Thank you and good job! 👏
import assign from 'lodash/assign';
, can we use object.assign()
instead?.Fields:
).@shahata almost ready to merge, could you review as well?
@odedcagan I updated the docs with the types of resolve
and prepare
Regarding the Object.assign
- It requires a polyfill so I thought its better to avoid using it
@tomerla looks good, @shahata ?
Very nice work @tomerla! Merging.
This is an early-stage PR for adding support for
resolve
property in the manifestMotivation: while the browsing is (lazy) downloading the manifest files, we can simultaneously do ajax request to fetch custom data before rendering the widget