0

Is there a way for React to take the back of an url user entered and pass it to a function? for example: www.site.com/foobar will pass the foobar to a function. Essencially what i'm trying to do is to run a check on foobar being in my database inside the checker function, if not there display 404 page not found.

const NotFound = () => (<h1>404.. This page is not found!</h1>)

class App extends Component {

  checker : function(e){
    if(foobar exists)
      //load page with data
    else
      // {NotFound}
  }

  render() {
    return (
      <Router history={hashHistory}>
        <Route path='/' component={LoginPage} />
        <Route path='*' component={this.checker()} />
      </Router>
    )
  }
}
5
  • foobar is an id? Commented Jun 6, 2017 at 22:57
  • no i'm planning on making a email verification system that generates a random token to put on the back of an url Commented Jun 6, 2017 at 22:57
  • yeah, like the route is actually /something/:someId and would handle /something/somereallylongtoken Commented Jun 6, 2017 at 22:58
  • depending on the version you're using, you can find what you're looking for here github.com/reactjs/react-router-tutorial/tree/master/lessons/… Commented Jun 6, 2017 at 22:59
  • right so something like .com/abcdefg and i thought i can use '*' to just accept any value which i can pass to a function to apply logic to determine whether or not it's a token within my database. Commented Jun 6, 2017 at 23:01

1 Answer 1

1

To expand what I had written in my comment -

class App extends React.Component {
  render() {
    return (
      <Router history={hashHistory}>
          <Route path='/' component={LoginPage} />
          <Route path='/:token' component={EmailValidation} />
      </Router>
    )
  }
}

class EmailValidation extends React.Component {
  state = { checked: false, valid: false }

  componentDidMount = () => {
    checkToken(this.props.params.token).then((valid) => {
      this.setState({ checked: true, valid })
    })
  }

  render() {
    const { checked, valid } = this.state

    return (
      <div>
        { checked
          ? <div>{ valid ? 'valid' : 'invalid' }</div>
          : <div>Checking token...</div> }
      </div>
    )
  }
}

this would be a good use case for an HoC which conditionally renders either the component you want or a 404 page - it would remove the binding between the 404 page and the email validate component (which are only sorta-kinda related)

if you're into using libraries, recompose has a bunch of nice helpers which can accomplish something like this for you.

something else you can do is use react-router's onEnter callback/prop although, iirc, you can't directly access props from that callback.

Sign up to request clarification or add additional context in comments.

1 Comment

It took me a while, but i managed to figure out how to pass the data along. Thank you for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.