Zuka Gaprindashvili
Zuka Gaprindashvili

Reputation: 209

react-native navigation(v5) deepLinking get params from URL

Have any ideas how can I get correct params ?

I getting this URL and I want navigate to correct screen within correct params.

URL -> /p-einhell-drill-screwdriver-123456/

I want to take form that url 3 parameters:

  1. URL which is -> p-einhell-drill-screwdriver-123456/
  2. seoName -> einhell-drill-screwdriver
  3. id -> 123456

I can take all of them in one parameter but i can not spread in 3 param. At this moment I have this parser but It takes only one params.

Forxample:

const config = {
     ProductScreen: {
                       path: ':seoName/',   /* p-einhell-drill-screwdriver-123456/ */
                       parse: {
                          seoName: (seoName) =>
                             seoName.split('-').slice(1, -1).join('-'),
                          URL: (seoName) => seoName,
                          currentProductId: (seoName) =>
                             seoName.split('-').pop(),
                       },
                    },
}

Result:

  params: {

  URL: undefined

  currentProductId: undefined

  seoName: "einhell-drill-screwdriver"    }

Upvotes: 0

Views: 1374

Answers (1)

satya164
satya164

Reputation: 10145

The options under parse refer to the params defined in the config (e.g. :seoName) and query params if any. You can't add arbitrary properties there.

But you can return an object in seoName:

parse: {
  seoName: URL => {
    const seoName = URL.split('-').slice(1, -1).join('-');
    const currentProductId = URL.split('-').pop();

    return { URL, seoName, currentProductId };
  }
}

And then get this object in route.params.seoName in your component.

Upvotes: 1

Related Questions