mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-15 09:34:19 +08:00
This commit is contained in:
@ -1,31 +1,43 @@
|
||||
import { useContext, useEffect } from 'react';
|
||||
import { useContext, useEffect, useRef } from 'react';
|
||||
|
||||
import { IonLifeCycleContext } from '../contexts/IonLifeCycleContext';
|
||||
import { IonLifeCycleContext, LifeCycleCallback } from '../contexts/IonLifeCycleContext';
|
||||
|
||||
export const useIonViewWillEnter = (callback: () => void) => {
|
||||
export const useIonViewWillEnter = (callback: LifeCycleCallback, deps: any[] = []) => {
|
||||
const context = useContext(IonLifeCycleContext);
|
||||
const id = useRef<number | undefined>();
|
||||
id.current = id.current || Math.floor(Math.random() * 1000000);
|
||||
useEffect(() => {
|
||||
callback.id = id.current!;
|
||||
context.onIonViewWillEnter(callback);
|
||||
}, []);
|
||||
}, deps);
|
||||
};
|
||||
|
||||
export const useIonViewDidEnter = (callback: () => void) => {
|
||||
export const useIonViewDidEnter = (callback: LifeCycleCallback, deps: any[] = []) => {
|
||||
const context = useContext(IonLifeCycleContext);
|
||||
const id = useRef<number | undefined>();
|
||||
id.current = id.current || Math.floor(Math.random() * 1000000);
|
||||
useEffect(() => {
|
||||
callback.id = id.current!;
|
||||
context.onIonViewDidEnter(callback);
|
||||
}, []);
|
||||
}, deps);
|
||||
};
|
||||
|
||||
export const useIonViewWillLeave = (callback: () => void) => {
|
||||
export const useIonViewWillLeave = (callback: LifeCycleCallback, deps: any[] = []) => {
|
||||
const context = useContext(IonLifeCycleContext);
|
||||
const id = useRef<number | undefined>();
|
||||
id.current = id.current || Math.floor(Math.random() * 1000000);
|
||||
useEffect(() => {
|
||||
callback.id = id.current!;
|
||||
context.onIonViewWillLeave(callback);
|
||||
}, []);
|
||||
}, deps);
|
||||
};
|
||||
|
||||
export const useIonViewDidLeave = (callback: () => void) => {
|
||||
export const useIonViewDidLeave = (callback: LifeCycleCallback, deps: any[] = []) => {
|
||||
const context = useContext(IonLifeCycleContext);
|
||||
const id = useRef<number | undefined>();
|
||||
id.current = id.current || Math.floor(Math.random() * 1000000);
|
||||
useEffect(() => {
|
||||
callback.id = id.current!;
|
||||
context.onIonViewDidLeave(callback);
|
||||
}, []);
|
||||
}, deps);
|
||||
};
|
||||
|
Reference in New Issue
Block a user