ReactContainer.tsx 798 B

123456789101112131415161718192021222324252627282930313233
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import coreModule from 'app/core/core_module';
  4. import { store } from 'app/stores/store';
  5. import { Provider } from 'mobx-react';
  6. function WrapInProvider(store, Component, props) {
  7. return (
  8. <Provider {...store}>
  9. <Component {...props} />
  10. </Provider>
  11. );
  12. }
  13. /** @ngInject */
  14. export function reactContainer($route, $location) {
  15. return {
  16. restrict: 'E',
  17. template: '',
  18. link(scope, elem) {
  19. let component = $route.current.locals.component;
  20. let props = {};
  21. ReactDOM.render(WrapInProvider(store, component, props), elem[0]);
  22. scope.$on('$destroy', function() {
  23. ReactDOM.unmountComponentAtNode(elem[0]);
  24. });
  25. },
  26. };
  27. }
  28. coreModule.directive('reactContainer', reactContainer);