Skip to content

Instantly share code, notes, and snippets.

@jeremy-donson
Last active January 12, 2018 14:20
Show Gist options
  • Save jeremy-donson/d9eae766cd90591015e1955d1162e30c to your computer and use it in GitHub Desktop.
Save jeremy-donson/d9eae766cd90591015e1955d1162e30c to your computer and use it in GitHub Desktop.
React.js: All desired urls redirect to 404, EXCEPT this pattern. (Blocker 1 of 2)
/*
There is a failed react test which is most pressing:
- /hello/conehead => Responds correctly with "Hello conehead." SUCCESS :)
- ** 404 page rediects work fine as a general rule, but the next item is an exception to that good rule..**
- /hello/cone/head? => Fails as desired, but does NOT get redirected to 404 page. :(
Since I am using react router 4 <Routes>...<Switch>...</Switch> </Routes>,
I believe that I need to extract location params, but that is clearly failing.
Then I could also improve my conditional logic which drives redirect to 404.
https://reacttraining.com/react-router/web/api/Switch
https://reacttraining.com/react-router/web/api/location
The winner first clue would be to show me how
to simply properly console.log the router.location elements (!!!!)
*/
import React from 'react';
import { Redirect, Match } from "react-router-dom";
export default ({ match: { params: { name }}}) => {
console.log(name); // debug router match or location params ??
if (name.indexOf("/") > -1)
{ return <Redirect to={{ pathname: "/404" }} />; } else
{return (<h2>Hello {name}</h2>);
}}
@jeremy-donson
Copy link
Author

if (name.indexOf("/") > -1) { return <Redirect to={{ pathname: "/404" }} />; } else
{return (

Hello {name}

);
that logic is wrong
need to console.log(thing);
then I can see what is up and how params are named etc
** kinda like args **
But params.... they are already separated, methinks...
/hello/Bob => $1 = hello, $2 = Bob
or sumthin
the / is not the condition to care about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment