乐闻世界logo
搜索文章和话题

What hook functions are included in Garfish's lifecycle management, and what are their purposes and execution order?

2月21日 16:01

Garfish provides a comprehensive lifecycle management mechanism to ensure the loading, mounting, updating, and unloading processes of sub-applications are controllable and predictable.

Lifecycle Hooks

1. bootstrap (Initialization)

  • Trigger: When a sub-application is first loaded
  • Purpose: Execute initialization logic for the sub-application, such as configuration loading and dependency injection
  • Executed Once: Called only once during the sub-application's lifecycle
  • Example:
javascript
export function bootstrap() { console.log('Sub-application initialized'); return Promise.resolve(); }

2. mount (Mounting)

  • Trigger: When a sub-application needs to be rendered to the page
  • Purpose: Render the sub-application into the specified container
  • Can Be Called Multiple Times: Triggered each time the route switches to this sub-application
  • Example:
javascript
export function mount(container) { ReactDOM.render(<App />, container); return Promise.resolve(); }

3. unmount (Unmounting)

  • Trigger: When a sub-application needs to be removed from the page
  • Purpose: Clean up sub-application resources including DOM, event listeners, timers, etc.
  • Must Execute: Ensure complete cleanup to avoid memory leaks
  • Example:
javascript
export function unmount(container) { ReactDOM.unmountComponentAtNode(container); return Promise.resolve(); }

4. update (Updating)

  • Trigger: When a sub-application needs to update (optional)
  • Purpose: Handle update logic for the sub-application
  • Not Required: Not all sub-applications need to implement this
  • Example:
javascript
export function update(props) { // Handle property updates return Promise.resolve(); }

Lifecycle Flow

  1. First Load: bootstrap → mount
  2. Route Switch: unmount (old app) → mount (new app)
  3. Reactivate: Directly call mount (bootstrap not repeated)
  4. Complete Unload: unmount + clean up all resources

Best Practices

  • Async Handling: All lifecycle functions should return Promises
  • Error Handling: Add error catching logic in lifecycle methods
  • Resource Cleanup: Thoroughly clean up all side effects in unmount
  • Performance Optimization: Avoid time-consuming operations in mount
  • State Management: Properly manage sub-application state to avoid repeated initialization

By properly using lifecycle hooks, you can ensure stable operation of sub-applications and effective resource management.

标签:Garfish