UNPKG

2.34 kBSource Map (JSON)View Raw
1{"version":3,"names":["useNavigationState","selector","navigation","useNavigation","setResult","React","useState","getState","selectorRef","useRef","useEffect","current","unsubscribe","addListener","e","data","state"],"sources":["useNavigationState.tsx"],"sourcesContent":["import type { NavigationState, ParamListBase } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport type { NavigationProp } from './types';\nimport useNavigation from './useNavigation';\n\ntype Selector<ParamList extends ParamListBase, T> = (\n state: NavigationState<ParamList>\n) => T;\n\n/**\n * Hook to get a value from the current navigation state using a selector.\n *\n * @param selector Selector function to get a value from the state.\n */\nexport default function useNavigationState<ParamList extends ParamListBase, T>(\n selector: Selector<ParamList, T>\n): T {\n const navigation = useNavigation<NavigationProp<ParamList>>();\n\n // We don't care about the state value, we run the selector again at the end\n // The state is only to make sure that there's a re-render when we have a new value\n const [, setResult] = React.useState(() => selector(navigation.getState()));\n\n // We store the selector in a ref to avoid re-subscribing listeners every render\n const selectorRef = React.useRef(selector);\n\n React.useEffect(() => {\n selectorRef.current = selector;\n });\n\n React.useEffect(() => {\n const unsubscribe = navigation.addListener('state', (e) => {\n setResult(selectorRef.current(e.data.state));\n });\n\n return unsubscribe;\n }, [navigation]);\n\n return selector(navigation.getState());\n}\n"],"mappings":";;;;;;;AACA;;AAGA;;;;;;;;AAMA;AACA;AACA;AACA;AACA;AACe,SAASA,kBAAT,CACbC,QADa,EAEV;EACH,MAAMC,UAAU,GAAG,IAAAC,sBAAA,GAAnB,CADG,CAGH;EACA;;EACA,MAAM,GAAGC,SAAH,IAAgBC,KAAK,CAACC,QAAN,CAAe,MAAML,QAAQ,CAACC,UAAU,CAACK,QAAX,EAAD,CAA7B,CAAtB,CALG,CAOH;;EACA,MAAMC,WAAW,GAAGH,KAAK,CAACI,MAAN,CAAaR,QAAb,CAApB;EAEAI,KAAK,CAACK,SAAN,CAAgB,MAAM;IACpBF,WAAW,CAACG,OAAZ,GAAsBV,QAAtB;EACD,CAFD;EAIAI,KAAK,CAACK,SAAN,CAAgB,MAAM;IACpB,MAAME,WAAW,GAAGV,UAAU,CAACW,WAAX,CAAuB,OAAvB,EAAiCC,CAAD,IAAO;MACzDV,SAAS,CAACI,WAAW,CAACG,OAAZ,CAAoBG,CAAC,CAACC,IAAF,CAAOC,KAA3B,CAAD,CAAT;IACD,CAFmB,CAApB;IAIA,OAAOJ,WAAP;EACD,CAND,EAMG,CAACV,UAAD,CANH;EAQA,OAAOD,QAAQ,CAACC,UAAU,CAACK,QAAX,EAAD,CAAf;AACD"}
\No newline at end of file