A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://ionicframework.com/docs/react/lifecycle below:

A Guide to Ionic React App Component Lifecycles

React Lifecycle

This guide discusses how to use the Ionic Lifecycle events in an Ionic React application.

Ionic provides a few lifecycle methods that you can use in your apps:

Event Name Description ionViewWillEnter Fired when the component routing to is about to animate into view. ionViewDidEnter Fired when the component routing to has finished animating. ionViewWillLeave Fired when the component routing from is about to animate. ionViewDidLeave Fired when the component routing from has finished animating.

These lifecycles are only called on components directly mapped by a router. This means if /pageOne maps to PageOneComponent, then Ionic lifecycles will be called on PageOneComponent but will not be called on any child components that PageOneComponent may render.

The way you access these methods varies based on if you are using class-based components or functional components. We cover both methods below.

to use the Ionic Lifecycle methods in a class-based component, you must wrap your component with the withIonLifeCycle higher order component (HOC) like so:

export default withIonLifeCycle(HomePage);

note

withIonLifeCycle is imported from @ionic/react

You can then create the appropriate lifecycle method on your class component, and the HOC calls that method when the event happens. Below is the entire component with each of the lifecycle methods implemented:

import React from 'react';
import { IonHeader, IonPage, IonToolbar, IonTitle, IonContent, withIonLifeCycle } from '@ionic/react';

class HomePage extends React.Component {
ionViewWillEnter() {
console.log('ionViewWillEnter event fired');
}

ionViewWillLeave() {
console.log('ionViewWillLeave event fired');
}

ionViewDidEnter() {
console.log('ionViewDidEnter event fired');
}

ionViewDidLeave() {
console.log('ionViewDidLeave event fired');
}

render() {
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Home</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent></IonContent>
</IonPage>
);
}
}

export default withIonLifeCycle(HomePage);

Ionic React exports hooks for each of the lifecycle methods that you can use in your functional components. Each of the hooks takes the method you want called when the event fires.

import {
IonContent,
IonHeader,
IonTitle,
IonToolbar,
useIonViewDidEnter,
useIonViewDidLeave,
useIonViewWillEnter,
useIonViewWillLeave,
} from '@ionic/react';
import React from 'react';

const HomePage: React.FC = () => {
useIonViewDidEnter(() => {
console.log('ionViewDidEnter event fired');
});

useIonViewDidLeave(() => {
console.log('ionViewDidLeave event fired');
});

useIonViewWillEnter(() => {
console.log('ionViewWillEnter event fired');
});

useIonViewWillLeave(() => {
console.log('ionViewWillLeave event fired');
});

return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Home</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent></IonContent>
</IonPage>
);
};

export default HomePage;

note

Functional components don't need to be wrapped with the withIonLifeCycle HOC as class components do.

Developers can also optionally pass reactive dependencies to each lifecycle hook. These are then passed to the underlying React useEffect hook:

const [data, setData] = useState('foo');

useIonViewDidEnter(() => {
console.log('ionViewDidEnter event fired');
}, [data]);

All the lifecycle methods in React (componentDidMount, componentWillUnmount, etc..) are available for you to use as well. However, since Ionic React manages the lifetime of a page, certain events might not fire when you expect them to. For instance, componentDidMount fires the first time a page is displayed, but if you navigate away from the page Ionic might keep the page around in the DOM, and a subsequent visit to the page might not call componentDidMount again. This scenario is the main reason the Ionic lifecycle methods exist, to still give you a way to call logic when views enter and exit when the native framework's events might not fire.

Below are some tips on use cases for each of the life cycle events.

Passing state between pages

Since Ionic React manages the lifetime of a page, state on previous pages may update as users navigate your application. This can impact state that is determined using useEffect from React or useLocation from React Router. For example, if PageA calls useLocation, the state of useLocation will change when the user navigates from PageA to PageB.

Developers should include the appropriate checks to ensure that previous pages only access defined states.

For example, the following code will error if testObject is not defined: { state.testObject.childKey }

Instead, developers should access childKey only if testObject is defined: { state.testObject?.childKey }


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4