react-use#useMountedState TypeScript Examples
The following examples show how to use
react-use#useMountedState.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts From logseq-plugin-tabs with MIT License | 7 votes |
useAppVisible = () => {
const [visible, setVisible] = useState(logseq.isMainUIVisible);
const isMounted = useMountedState();
React.useEffect(() => {
const eventName = "ui:visible:changed";
const handler = async ({ visible }: { visible: boolean }) => {
if (isMounted()) {
setVisible(visible);
}
};
logseq.on(eventName, handler);
return () => {
logseq.off(eventName, handler);
};
}, [isMounted]);
return visible;
}
Example #2
Source File: utils.ts From logseq-plugin-tabs with MIT License | 7 votes |
useSidebarVisible = () => {
const [visible, setVisible] = useState(false);
const isMounted = useMountedState();
React.useEffect(() => {
logseq.App.onSidebarVisibleChanged(({ visible }) => {
if (isMounted()) {
setVisible(visible);
}
});
}, [isMounted]);
return visible;
}
Example #3
Source File: utils.ts From logseq-plugin-tabs with MIT License | 7 votes |
useThemeMode = () => {
const isMounted = useMountedState();
const [mode, setMode] = React.useState<"dark" | "light">("light");
React.useEffect(() => {
setMode(
(top!.document
.querySelector("html")
?.getAttribute("data-theme") as typeof mode) ??
(matchMedia("prefers-color-scheme: dark").matches ? "dark" : "light")
);
return logseq.App.onThemeModeChanged((s) => {
if (isMounted()) {
setMode(s.mode);
}
});
}, [isMounted]);
return mode;
}
Example #4
Source File: withOwner.tsx From rcvr-app with GNU Affero General Public License v3.0 | 5 votes |
withOwner =
(userConfig: WithOwnerConfig = {}) =>
(ComposedComponent: React.FC) => {
const config = { ...defaultConfig, ...userConfig }
const WithOwnerComp = (props) => {
const isMounted = useMountedState()
const [renderPage, setRenderPage] = React.useState(false)
const { data, status, error } = useOwner()
const router = useRouter()
const publicKey = data?.publicKey
React.useEffect(() => {
if (!isMounted()) return
if (status === 'error' && config.redirect === 'unauthorized') {
router.replace('/business/login')
return
}
if (status === 'error') {
setRenderPage(true)
return
}
if (status === 'success' && config.redirect === 'authorized') {
if (!publicKey) {
router.replace('/business/setup/success')
} else {
router.replace('/business/dashboard')
}
return
}
if (status === 'success') {
setRenderPage(true)
return
}
}, [router, status, error, isMounted, publicKey])
return (
<>
<PageTitle />
{renderPage ? (
<ComposedComponent {...props} owner={data} />
) : (
<Loading show />
)}
</>
)
}
WithOwnerComp.displayName = `withOwner(${ComposedComponent.displayName})`
return WithOwnerComp
}