Просмотр исходного кода

ref(ui) Remove createReactClass from Hook component. (#26384)

Remove another instance and use unsubscribe callbacks.
Mark Story 3 лет назад
Родитель
Сommit
7171aebd28
1 измененных файлов с 21 добавлено и 11 удалено
  1. 21 11
      static/app/components/hook.tsx

+ 21 - 11
static/app/components/hook.tsx

@@ -1,6 +1,4 @@
 import * as React from 'react';
 import * as React from 'react';
-import createReactClass from 'create-react-class';
-import Reflux from 'reflux';
 
 
 import HookStore from 'app/stores/hookStore';
 import HookStore from 'app/stores/hookStore';
 import {HookName, Hooks} from 'app/types/hooks';
 import {HookName, Hooks} from 'app/types/hooks';
@@ -17,6 +15,10 @@ type Props<H extends HookName> = {
   children?: (opts: {hooks: Array<Hooks[H]>}) => React.ReactNode;
   children?: (opts: {hooks: Array<Hooks[H]>}) => React.ReactNode;
 } & Omit<Parameters<Hooks[H]>[0], 'name'>;
 } & Omit<Parameters<Hooks[H]>[0], 'name'>;
 
 
+type HookState<H extends HookName> = {
+  hooks: Array<Hooks[H]>;
+};
+
 /**
 /**
  * Instead of accessing the HookStore directly, use this.
  * Instead of accessing the HookStore directly, use this.
  *
  *
@@ -32,13 +34,16 @@ type Props<H extends HookName> = {
  *   </Hook>
  *   </Hook>
  */
  */
 function Hook<H extends HookName>({name, ...props}: Props<H>) {
 function Hook<H extends HookName>({name, ...props}: Props<H>) {
-  const HookComponent = createReactClass({
-    displayName: `Hook(${name})`,
-    mixins: [Reflux.listenTo(HookStore, 'handleHooks') as any],
+  class HookComponent extends React.Component<{}, HookState<H>> {
+    static displayName = `Hook(${name})`;
+
+    state = {
+      hooks: HookStore.get(name).map(cb => cb(props)),
+    };
 
 
-    getInitialState() {
-      return {hooks: HookStore.get(name).map(cb => cb(props))};
-    },
+    componentWillUnmount() {
+      this.unsubscribe();
+    }
 
 
     handleHooks(hookName: HookName, hooks: Array<Hooks[H]>) {
     handleHooks(hookName: HookName, hooks: Array<Hooks[H]>) {
       // Make sure that the incoming hook update matches this component's hook name
       // Make sure that the incoming hook update matches this component's hook name
@@ -47,7 +52,12 @@ function Hook<H extends HookName>({name, ...props}: Props<H>) {
       }
       }
 
 
       this.setState({hooks: hooks.map(cb => cb(props))});
       this.setState({hooks: hooks.map(cb => cb(props))});
-    },
+    }
+
+    unsubscribe = HookStore.listen(
+      (hookName: HookName, hooks: Array<Hooks[H]>) => this.handleHooks(hookName, hooks),
+      undefined
+    );
 
 
     render() {
     render() {
       const {children} = props;
       const {children} = props;
@@ -61,8 +71,8 @@ function Hook<H extends HookName>({name, ...props}: Props<H>) {
       }
       }
 
 
       return this.state.hooks;
       return this.state.hooks;
-    },
-  });
+    }
+  }
 
 
   return <HookComponent />;
   return <HookComponent />;
 }
 }