A web view Mini App has two lifecycles: the shell mini-app's, and your web app's. This page covers your web app's — what it can listen for, and what belongs in each.
onLaunch, onShow, onHide, onLoad and the rest. See Native mini-app → Lifecycle.ft.miniProgram.onShow, onHide and onUnload — plus your normal browser events.Your web app is a web page, so browser events like load run once, when it first opens. They don't tell you when the user leaves and comes back. The SDK adds three events for that:
ft.miniProgram.onShowft.miniProgram.onHideft.miniProgram.onUnloadRegister each event once, when your web app starts:
// Register once, when your web app starts — not on every render.
if (window.ft && ft.miniProgram) {
ft.miniProgram.onShow(function () {
// Your web app is visible again — refresh what may have changed.
refreshUserAndCart();
});
ft.miniProgram.onHide(function () {
// Your web app is no longer visible — pause what doesn't need to run.
pauseTimersAndMedia();
});
ft.miniProgram.onUnload(function () {
// The page holding your web app is closing — save what must not be lost.
saveDraft();
});
}In a React web app, do it once in your top-level component:
// React — register once, in your top-level component.
import { useEffect } from 'react';
export default function App() {
useEffect(() => {
if (!window.ft || !ft.miniProgram) return; // opened outside the Mini App
ft.miniProgram.onShow(() => refreshUserAndCart());
ft.miniProgram.onHide(() => pauseTimersAndMedia());
ft.miniProgram.onUnload(() => saveDraft());
}, []); // [] — once, on start
return <Routes />;
}ft first. It only exists inside the Mini App. Opened in a normal browser, there is no ft, and calling it would throw. See SDK → Knowing you are inside a web view.When your web app has finished loading, call ft.complete(). If the shell sets a load timeout for the web-view, a web app that never calls it is treated as having failed — no matter how well it rendered. See SDK.
loadft.miniProgram.onShowft.miniProgram.onHideft.miniProgram.onUnload