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:
javascriptexport 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:
javascriptexport 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:
javascriptexport 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:
javascriptexport function update(props) { // Handle property updates return Promise.resolve(); }
Lifecycle Flow
- First Load: bootstrap → mount
- Route Switch: unmount (old app) → mount (new app)
- Reactivate: Directly call mount (bootstrap not repeated)
- 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.