ReactContainer.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Provider } from 'mobx-react';
  4. import coreModule from 'app/core/core_module';
  5. import { store } from 'app/stores/store';
  6. import { BackendSrv } from 'app/core/services/backend_srv';
  7. import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
  8. function WrapInProvider(store, Component, props) {
  9. return (
  10. <Provider {...store}>
  11. <Component {...props} />
  12. </Provider>
  13. );
  14. }
  15. /** @ngInject */
  16. export function reactContainer($route, $location, backendSrv: BackendSrv, datasourceSrv: DatasourceSrv) {
  17. return {
  18. restrict: 'E',
  19. template: '',
  20. link(scope, elem) {
  21. let component = $route.current.locals.component;
  22. // Dynamic imports return whole module, need to extract default export
  23. if (component.default) {
  24. component = component.default;
  25. }
  26. const props = {
  27. backendSrv: backendSrv,
  28. datasourceSrv: datasourceSrv,
  29. };
  30. ReactDOM.render(WrapInProvider(store, component, props), elem[0]);
  31. scope.$on('$destroy', function() {
  32. ReactDOM.unmountComponentAtNode(elem[0]);
  33. });
  34. },
  35. };
  36. }
  37. coreModule.directive('reactContainer', reactContainer);