GOOGLE ADS

jueves, 28 de abril de 2022

El enrutamiento de enlaces profundos nativos de React no funciona

Estoy usando enlaces dinámicos para enlaces profundos.

const linking = {
prefixes: [
www.example.com
],
config: {
screens: {
Chat: {
path: ":id",
parse: {
id: (id) => `${id}`,
},
},
Profile: 'user',
},
},
};
function App() {
return (
<NavigationContainer linking={linking} fallback={<Text>Loading...</Text>}>
<Stack.Screen name="Chat" component={ChatScreen} />
<Stack.Screen name="Profile" component={ProfileScreen} />
</NavigationContainer>
);
}

www.example.com/usersiempre ruta a la ChatScreenpantalla. Quiero enrutar en ProfileScreen. ¿Cómo manejar el archivo de configuración en la vinculación?


Solución del problema

Establece tu configdebería como

const config = {
screens: {
Profile: '/user',
Chat:'/chat/:id'
},
};

tu linkingdeberías ser

const linking = {
prefixes: [
'www.example.com',
],
config,
};

Al hacerlo, www.example.com/userse enrutará al ProfileScreencomponente. www.example.com/chat/:<some id>lo llevará a ChatScreencon la ruta param como id.

Actualización: parece que está intentando ChatScreenpasar www.example.com/<some id>y también tiene www.example.com/userque cargar ProfileScreen. El problema es que la navegación de React considera la cadena "user"como parámetro idy lo lleva a ChatScreensí mismo. Esa es la razón por la que agregué "chat" en la ruta de ChatScreen.

Si realmente desea usar www.example.com/<some id>to load ChatScreen, puede usar getStateFromPathy escribir su propia lógica para diferenciar entre ID y nombres de ruta. Para hacer eso, tu linkingvoluntad será

 const linking = {
prefixes: [
'www.example.com',
],
config,
getStateFromPath: (path, options) => {
if (path ==='/user') {
return {routes: [{ name: 'Profile' }],
};
} else {
return {routes: [{ name: 'Chat',params:{id:getIdFromPath(path)} }],
};
}
},
};

aquí checkIfValidPathes para verificar si la ruta de la URL es real ido "usuario". getIdFromPathes extraer id de la url. No he probado esto, pero esto debería funcionar.

const checkIfValidPath=(path)=>{
return path!=='www.example.com/user';
}
const getIdFromPath =(path)=>{
return path.str.slice(16, str.length);
// 16 is the length of "www.example.com/", you probably should the number according to your url.
}

No hay comentarios.:

Publicar un comentario

Flutter: error de rango al acceder a la respuesta JSON

Estoy accediendo a una respuesta JSON con la siguiente estructura. { "fullName": "FirstName LastName", "listings...