useRouter
tsimport {useRouter } from 'solito/router'
tsimport {useRouter } from 'solito/router'
This hook lets you navigate from one screen to another across platforms, using nothing but URLs and Next.js Url objects.
Usage​
tsxconst {push ,replace ,back ,parseNextPath } =useRouter ()ÂconstonPress = () => {push ('/')}constonGoBack = () => {back ()}
tsxconst {push ,replace ,back ,parseNextPath } =useRouter ()ÂconstonPress = () => {push ('/')}constonGoBack = () => {back ()}
Returns​
push​
Follows the exact same API as push returned by Next.js useRouter hook.
Basic example​
tsxconst {push } =useRouter ()ÂconstonOpenArtist = () => {push ('/artists/drake')Â// or, you can do:push ({pathname : '/artists/[slug]',query : {slug : 'drake',},})}
tsxconst {push } =useRouter ()ÂconstonOpenArtist = () => {push ('/artists/drake')Â// or, you can do:push ({pathname : '/artists/[slug]',query : {slug : 'drake',},})}
Passing query parameters​
Use the query object to pass any query parameters to the next screen.
tsxconst {push } =useRouter ()ÂconstonOpenArtist = () => {push ({pathname : '/artists/drake',query : {initialOfferAmount : 500000,},})}
tsxconst {push } =useRouter ()ÂconstonOpenArtist = () => {push ({pathname : '/artists/drake',query : {initialOfferAmount : 500000,},})}
solito will automatically stringify this into a URL for you on Native, making it easy for React Navigation.
replace​
Just like push(), this follows the exact same API as Next.js useRouter().replace.
It takes the exact same arguments as push.
tsxconst {replace } =useRouter ()ÂconstonOpenArtist = () => {replace ({pathname : '/artists/drake',query : {initialOfferAmount : 500000,},})}
tsxconst {replace } =useRouter ()ÂconstonOpenArtist = () => {replace ({pathname : '/artists/drake',query : {initialOfferAmount : 500000,},})}
Stack Replace on Native​
Solito v2 introduces a new nativeBehavior option in the router.replace() function to let you use a stack replacement action on native navigators:
tsimport { useRouter } from 'solito/router'export function Home() {const { replace } = useRouter()const openArtists = () => {replace('/artists', undefined, {experimental: {nativeBehavior: 'stack-replace',isNestedNavigator: true,},})}// ...}
tsimport { useRouter } from 'solito/router'export function Home() {const { replace } = useRouter()const openArtists = () => {replace('/artists', undefined, {experimental: {nativeBehavior: 'stack-replace',isNestedNavigator: true,},})}// ...}
By passing nativeBehavior: 'stack-replace', you tell Solito to re-write your navigation action with StackActions.replace(). The isNestedNavigator property is an important way to indicate to Solito that your stack is nested inside of another navigator, such as tabs. Chances are, this property will always be true.
You can also use this functionality on the Link and TextLink components when replace is set to true:
tsx<Linkhref="/artists"replaceexperimental={{nativeBehavior: 'stack-replace',isNestedNavigator: true,}}/>
tsx<Linkhref="/artists"replaceexperimental={{nativeBehavior: 'stack-replace',isNestedNavigator: true,}}/>
For now, this feature will live behind the experimental flag.
parseNextPath​
This function is used under the hood by push and replace to turn a Next.js Url object (or string) into a valid URL string. It's exposed in case you're doing something special.
It takes a Next.js Url object (or a string) and returns a stringified URL.
It's useful if you want to provide stable keys to lists of URLs, for instance.
API​
tsparseNextPath(url: string | Url): string
tsparseNextPath(url: string | Url): string
tsconstpath =parseNextPath ({pathname : '/artists',})
tsconstpath =parseNextPath ({pathname : '/artists',})
Arguments​
It has a single argument, which is either a string or a Url object. A Url object is the same as the first argument for push.
Returns​
string
Usage​
Imagine this example:
tsconst {parseNextPath } =useRouter ()Âconsthref =parseNextPath ({pathname : '/artists/[slug]',query : {slug : 'drake',},})
tsconst {parseNextPath } =useRouter ()Âconsthref =parseNextPath ({pathname : '/artists/[slug]',query : {slug : 'drake',},})
Here, href will become /artists/drake.
If you add other query parameters, they will be added to the end of the URL:
tsconst {parseNextPath } =useRouter ()Âconsthref =parseNextPath ({pathname : '/artists/[slug]',query : {slug : 'drake',initialOfferAmount : 500_000,},})
tsconst {parseNextPath } =useRouter ()Âconsthref =parseNextPath ({pathname : '/artists/[slug]',query : {slug : 'drake',initialOfferAmount : 500_000,},})
In this case, href is /artists/drake?initialOfferAmount=500000.
Basic example​
tsxconst {parseNextPath } =useRouter ()Âconstlist =artists .map ((artist ) => {consthref =parseNextPath ({pathname : '/artists/[slug]',query : {slug :artist .slug ,},})Âreturn <Artist href ={href }key ={artist .slug } />})
tsxconst {parseNextPath } =useRouter ()Âconstlist =artists .map ((artist ) => {consthref =parseNextPath ({pathname : '/artists/[slug]',query : {slug :artist .slug ,},})Âreturn <Artist href ={href }key ={artist .slug } />})