如何使用 React-Navigation 深度链接将参数作为对象传递?
How to pass param as an object with React-Navigation deep linking?
我使用 React-Navigation 深度链接从 Url 获取参数,但我想将这些参数传递给一个对象。现在我这样做了:
prefixes: [Linking.makeUrl('/'), 'https://test.app', 'https://*.test.app'],
config: {
screens: {
App: {
screens: {
Chat: {
path: 'chat/:firstName/:lastName/:birthdate',
parse: {
firstName: (firstName: string) => decodeURIComponent(firstName),
lastName: (lastName: string) => decodeURIComponent(lastName),
birthdate: (birthdate: string) => decodeURIComponent(birthdate),
},
},
这是结果:
const { firstName, lastName, birthdate} = route.params
我需要的是一个包含名字、姓氏、生日的对象:
const { userObject } = route.params
您可以使用 getStateFromPath 随意配置参数。
像这样的东西应该有用。注意:我还没有测试过嵌套屏幕。您可能需要稍微修改一下以处理嵌套屏幕。
import { getStateFromPath } from '@react-navigation/native';
const linking = {
prefixes: [Linking.makeUrl('/'), 'https://test.app', 'https://*.test.app'],
config: {
screens: {
App: {
screens: {
Chat: {
path: 'chat/:firstName/:lastName/:birthdate',
parse: {
firstName: (firstName: string) => decodeURIComponent(firstName),
lastName: (lastName: string) => decodeURIComponent(lastName),
birthdate: (birthdate: string) => decodeURIComponent(birthdate),
},
},
},
},
},
},
getStateFromPath: (path, options) => {
const state = getStateFromPath(path, options);
const newState = {
...state,
routes: states.routes.map(route => {
if (route.name === 'Chat') {
// modify your params however you like here!
return {
...route,
params: { userObject: route.params }
}
} else {
return route
}
}),
};
return newState;
},
}
我使用 React-Navigation 深度链接从 Url 获取参数,但我想将这些参数传递给一个对象。现在我这样做了:
prefixes: [Linking.makeUrl('/'), 'https://test.app', 'https://*.test.app'],
config: {
screens: {
App: {
screens: {
Chat: {
path: 'chat/:firstName/:lastName/:birthdate',
parse: {
firstName: (firstName: string) => decodeURIComponent(firstName),
lastName: (lastName: string) => decodeURIComponent(lastName),
birthdate: (birthdate: string) => decodeURIComponent(birthdate),
},
},
这是结果:
const { firstName, lastName, birthdate} = route.params
我需要的是一个包含名字、姓氏、生日的对象:
const { userObject } = route.params
您可以使用 getStateFromPath 随意配置参数。
像这样的东西应该有用。注意:我还没有测试过嵌套屏幕。您可能需要稍微修改一下以处理嵌套屏幕。
import { getStateFromPath } from '@react-navigation/native';
const linking = {
prefixes: [Linking.makeUrl('/'), 'https://test.app', 'https://*.test.app'],
config: {
screens: {
App: {
screens: {
Chat: {
path: 'chat/:firstName/:lastName/:birthdate',
parse: {
firstName: (firstName: string) => decodeURIComponent(firstName),
lastName: (lastName: string) => decodeURIComponent(lastName),
birthdate: (birthdate: string) => decodeURIComponent(birthdate),
},
},
},
},
},
},
getStateFromPath: (path, options) => {
const state = getStateFromPath(path, options);
const newState = {
...state,
routes: states.routes.map(route => {
if (route.name === 'Chat') {
// modify your params however you like here!
return {
...route,
params: { userObject: route.params }
}
} else {
return route
}
}),
};
return newState;
},
}