11.4.6 • Published 1 year ago

react-component-controller v11.4.6

Weekly downloads
7
License
UNLICENSED
Repository
github
Last release
1 year ago

react-component-controller

Core concepts:

  • Controller holds all business logic for the component. Components itself must be as dumb as possible. Component can hold state of something like dropdown is open or not via React.useState();

  • Before the first component render optional onInit is called on the controller, if present. If it returns function, it will be called upon component unmount. This unmount callback can be explicitly described in onDestroy optional function.

  • what was passed as argument for createHookWithContext will be accessible as this.ctx inside controller class. usually the result of it is exported and used as useController() React hook later.

  • if props needed, pass them as 2-nd argument to useController(ComponentController, props). They will be accessible as this.props. Also it might be the case for tests, if controller is passed as prop to component, useController will use it instead of creating new controller instance.

  • props are updated on every component call.
  • Controllers can be re-used inside other components or controllers via createReactController()
  • Controller can be passed down the line of components as a prop, use UseReactController typing to type this prop in consumers.

Usage (mobx example)

// file ./ComponentController.ts
export class ComponentController extends Controller {
  @observable someProp: number;
  onInit = () => {
    this.ctx.appStore.someFunction();
    runInAction(() => this.someProp = 1);
  };
}

// file ./ServicesContext.ts
export const ServicesContext: React.Context<Services> = React.createContext(null);

// file ./Controller.ts
export const useController = createHookWithContext(ServicesContext);
export class Controller<P = any> extends ReactController<Services, P> {}

// file ./Component.tsx
export const Component: React.FC = observer(() => {
  const controller = useController(ComponentController);
  return (
    <div>{controller.someProp}</div>
  );
});
11.4.6

1 year ago

11.4.2

2 years ago

11.4.3

2 years ago

11.4.0

2 years ago

11.4.1

2 years ago

11.4.4

2 years ago

11.4.5

2 years ago

11.3.0

2 years ago

11.2.0

3 years ago

11.2.1

3 years ago

11.1.0

3 years ago

10.0.0

4 years ago

11.0.0

3 years ago

8.1.0

4 years ago

8.0.0

4 years ago

9.0.0

4 years ago

7.0.0

4 years ago

6.0.3

4 years ago

6.0.4

4 years ago

6.0.0

4 years ago

6.0.2

4 years ago

4.0.0

4 years ago

5.0.0

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago