useLocationSelector
Selectively retrieve properties from location. This hook will only re-render when the value you return is changed. For more information, see
Usage
Live Editor
function CurrentPathname() { const pathname = useLocationSelector(location => location.pathname); const ref = useRef(0); useEffect(() => { ref.current = ref.current + 1; }); return ( <div> {pathname} <div>renderCount: {ref.current}</div> </div> ); } function CurrentHash() { const hash = useLocationSelector(location => location.hash || "nohash"); const ref = useRef(0); useEffect(() => { ref.current = ref.current + 1; }); return ( <div> {hash} <div>hashRender: {ref.current}</div> </div> ); } function Links() { return ( <div> <a href="#link1">#link1</a> <a href="#link2">#link2</a> <a href="#link3">#link3</a> </div> ); } function Demo() { return ( <div> <CurrentPathname /> <CurrentHash /> <Links /> </div> ); } render(<Demo/>)
Result
Loading...
API
useLocationSelector
Returns
R | undefined
Arguments
Argument | Description | Type | DefaultValue |
---|---|---|---|
selector | selector function | (location: Location) => R (Required) | - |
fallback | default value | R | undefined | - |