ReactContainer.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. routeParams: $route.current.params,
  30. };
  31. ReactDOM.render(WrapInProvider(store, component, props), elem[0]);
  32. scope.$on('$destroy', function() {
  33. ReactDOM.unmountComponentAtNode(elem[0]);
  34. });
  35. },
  36. };
  37. }
  38. coreModule.directive('reactContainer', reactContainer);