UNPKG

@react-navigation/core

Version:

Core utilities for building navigators

39 lines (38 loc) 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useIsFocused; var React = _interopRequireWildcard(require("react")); var _useNavigation = _interopRequireDefault(require("./useNavigation")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * Hook to get the current focus state of the screen. Returns a `true` if screen is focused, otherwise `false`. * This can be used if a component needs to render something based on the focus state. */ function useIsFocused() { const navigation = (0, _useNavigation.default)(); const [isFocused, setIsFocused] = (0, React.useState)(navigation.isFocused); const valueToReturn = navigation.isFocused(); if (isFocused !== valueToReturn) { // If the value has changed since the last render, we need to update it. // This could happen if we missed an update from the event listeners during re-render. // React will process this update immediately, so the old subscription value won't be committed. // It is still nice to avoid returning a mismatched value though, so let's override the return value. // This is the same logic as in https://github.com/facebook/react/tree/master/packages/use-subscription setIsFocused(valueToReturn); } React.useEffect(() => { const unsubscribeFocus = navigation.addListener('focus', () => setIsFocused(true)); const unsubscribeBlur = navigation.addListener('blur', () => setIsFocused(false)); return () => { unsubscribeFocus(); unsubscribeBlur(); }; }, [navigation]); React.useDebugValue(valueToReturn); return valueToReturn; } //# sourceMappingURL=useIsFocused.js.map