@modular-component/with-lifecycle
TypeScript icon, indicating that this package has built-in type declarations

0.2.3 • Public • Published

@modular-component/with-lifecycle

Basic stage allowing to save the result of a custom hook to a lifecycle argument passed down to further stages. The lifecycle hook receives the previous argument map as parameter, and can therefore use props or any other injected argument.

Usage

import { ModularComponent } from '@modular-component/core'
import { lifecycle } from '@modular-component/with-lifecycle'

const MyComponent = ModularComponent()
  .with(lifecycle(({ props }) => {
    // Write component state & logic here
  }))
  .with(render(({ props, lifecycle }) => (
    // Use computed lifecycle in the render phase
  )))

Implementation

with(lifecycle) receives a function taking the current arguments map as parameter. It uses this function as the stage hook directly:

import { ModularStage } from '@modular-component/core'

export function lifecycle<Args extends {}, Return>(
  useLifecycle: (args: Args) => Return,
): ModularStage<'lifecycle', (args: Args) => Return> {
  return { field: 'lifecycle', useStage: useLifecycle }
}

Package Sidebar

Install

npm i @modular-component/with-lifecycle

Weekly Downloads

5

Version

0.2.3

License

MIT

Unpacked Size

5.68 kB

Total Files

9

Last publish

Collaborators

  • jvdsande