Ionic 4/Angular 7 lifecycle hooks called just one time per page - hook

In ionic 3 there was lifecycle events like ionViewWillEnter and ionViewWillLeave. But in Ionic 4 this event must be replaced by Angular lifecycle events like ngOnInit and ngOnDestroy.
In my application I need to realize some given operations when the user enter or leaves a page. For example, if the user push another page (navigate forward in Ionic 4), the app whould execute ionViewWillLeave for the current page before the new page being pushed. If the user came back from a pushed page (navigate backward in Ionic 4) the app whould execute ionViewWillEnter for the previous page.
I tried to replace this events with ngOnInit and ngOnDestroy. But this events are called just one time each per page. For example, if I route to another page (navigate root in Ionic 4) and came back to the first page, the first page the ngOnInit and ngOnDestroy events will not be called again.
How can I call a event each time a user enter or leaves a page?

I made a mistake. The documentation says:
With V4, we're now able to utilize the typical events provided by
Angular. But for certain cases, you might want to have access to the
events fired when a component has finished animating during it's route
change. In this case, the ionViewWillEnter, ionViewDidEnter,
ionViewWillLeave, and ionViewDidLeave have been ported over from V3.
Use these events to coordinate actions with Ionic's own animations
system.
Older events like ionViewDidLoad, ionViewCanLeave, and ionViewCanEnter
have been removed, and the proper Angular alternatives should be used.
Migration Guide - Lifecycle Events

**
Lifecycles in Ionic 4
**
Ionic 4 extends the Router Navigation of Angular
Ionic 4 introduces a stack functionality (like in Ionic 3)
Ionic 4 adds new Lifecycle Hooks to Angular:
ionViewWillEnter — Fired when entering a page (also if it’s come back from stack)
ionViewDidEnter — Fired after entering (also if it’s come back from stack)
ionViewWillLeave — Fired if the page will leaved (also if it’s keep in stack)
ionViewDidLeave — Fired after the page was leaved (also if it’s keep in stack)
ionViewWillUnload — In Angular not firing because here you have to use ngOnDestroy
Except ionViewDidLoad (because it’s the same like ngOnInit) and the two nav guards all Lifecycle hooks from Ionic 3 are still available
ngOnInit will not be triggered, if you come back to a page after putting it into a stack
By default, if you navigate forward, the current page keeps in stack, so NO ngOnDestroy will be triggered. Only if you set the new page as root (navController.navigateRoot())) or you navigate backwards, it will be removed from stack
If you want to cancel Observables, just do it ionViewWillLeave or ionViewDidLeave and subscribe to it again in ionViewWillEnter or ionViewDidEnter
Take a look at the DOM inspector, there you can see that your page is still in the stack
If you use the Angular Router, pages will add to the stack. I recommend to use the Ionic Angular NavController because here you can use the new stack functionality

I feel this is the problem you need, you can refer to my solution.(ionic 6 , angular 13).
Ionic Globally Call IonViewWillEnter on App.component.ts

Related

How to reload Angular app (All Pages) after every few seconds

I'm currently working on Angular,Mongodb and nodejs as backend. But problem is whenever I add something like image or any entry I have to refresh the angular page to show the changes.
So is there any way to automatically reload every page of an angular app?
Also I would like to add that it's a group project so if another partner enter any image (image path) into data from his/her side I dont get the image as image doesn't not existed in my laptop and visa versa.
Is there any other way to reflect the image both the side even if it doesn't exist in each other's memory ?
You could always add a reload function on a timer:
setTimeout(window.location.reload.bind(window.location), 250);
But I would argue that this method isn't conducive to a good user experience though - window reloads are jarring.
A better option would be to create an observable bound to changes in collections on your database, and reset the image holder HTML based on events from that observable.

Use socket.io in react and node js application

I am using reactjs and nodejs for my application. I am calling the api created in nodejs in my react js component. Let's suppose, I have a component User. And i am calling api in componentWillMount function to get a list of users. So when a new user is added by add user component, The change is not reflecting to list of users. So i need to use socket.io to make application real time, so that my list of users can get something is updated in database and its time to render the component again.
Is there anything regarding above?
You could use Socket.io for updating the users list. Another option is to use server-sent events (for more info on the differences between the two, see this answer).
If you choose to use Socket.io, you can emit an event from the client once a new user is added. Once the server receives that event, it broadcasts it to all other connected sockets (using socket.broadcast.emit). The sockets listen to the users-update event (an example for this can be found here), and once they receive it, the client should update the User component and rerender it.
If you choose to use SSE, check out this article.
If you want to apply data from response of api to rendered component, I recommend you will call api on componentDidMount.
It is the great way.
Of course, you can use socket.io

React Native Navigation library showModal, push, pop relating to the React Native lifecycle

Using the 'react native navigation' library
Short:
How does the react native navigator library relate to the react native life cycle. I know it's built on top of native code. It seems you could call showModal, push, pop from several lifecycle steps (componentDidMount, componentDidUpdate, ...). Is there a best practice and what are the reasonings.
Details:
I'm currently using react native navigation to build on onboarding flow. During my onboarding flow (midway) there is a trigger for a secondary flow to launch. At this point I open a new modal and push a screen (screenB) into modal #2. I'm running into the issue where screenB will not render unless I enabled JS debug and set a break point within screenB . The break point can be anywhere in that file so long as it gets hit. I have retested this many times and can only get screenB to render if a break point is set.

Laravel echo server console says left the channel and again joins the channel while navigating through pages?

I am trying to add realtime functionality to my laravel app which is being developed in laravel-5.4.
So, I have implemented a simple chat example from the documentation
that is laravel-echo-server with redis and socket.io. It’s working fine but I have noticed a weird behaviour that is when the user navigates to different pages across the application, the laravel-echo-server console says left the channel and again joined the channel so is it intended behaviour and what is the best practise to use it. am i doing something wrong here? I don't want him to left the channel, instead I want to stay him at the same connection.
If you change page, everything is lost and a new content is dowloaded from the server. So it's normal that the user "left the channel" then re"joined the channel". It's the intended behavior.
If you don't want this kind of behavior, you have to stay one the same page (so implementing something like a single page application). Because when you leave a page to go to another, current scripts stops then a new world is re-created.

Why do multiple requests to ServiceStack http handlers fail?

I've verified this behavior with multiple projects, including the Windows Service AppHost Starter Template example project (unmodified). Initial requests succeed and display the default TODO page, but subsequent requests never complete.
It's simple to reproduce this scenario. Just perform the first request to http://localhost:83. You'll see the default TODO page render fine. Now hit "refresh" in the browser and you should now see the request never come through. If running in debug mode through VS, you'll see ObjectDisposedExceptions in the Output window.
Is there a problem with the latest ServiceStack codebase?
There was an issue in the response stream not being closed properly.
Should now be fixed in the latest version. Can you try again with the latest version v3.07, either on:
https://github.com/ServiceStack/ServiceStack.Examples/downloads
https://github.com/ServiceStack/ServiceStack/downloads
or 'ServiceStack' on NuGet.

Resources