Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - inserting variable into Route path

Tags:

reactjs

meteor

I have an app that I am creating and am wondering how you would insert variables into the <Route path={insert variable here} component={myProfile}> I am trying to create a myProfile page and I am trying to get it so when they click onto the link, it redirects them to http://mywebsite.com/userId but when I try to create a Route with a variable in the path argument, it does not return the component I am trying to render when on that path.

routes.js

import { Meteor } from "meteor/meteor"
import React from "react";
import { withRouter, Switch, BrowserRouter, Route, Redirect, Link } from "react-router-dom";

import Login from "../ui/authentication/Login";
import Signup from "../ui/authentication/Signup";
import Home from "../ui/Home";
import { SubjectRoutes } from "../ui/subjectRoutes/subjectRoutes";
import AddNote from "../ui/AddNote";
import myProfile from "../ui/myProfile";
import NotFound from "../ui/NotFound";

export default class Routes extends React.Component{
  renderSubjectRoutes(subjects){
    return subjects.map((subject) => {
      return <Route key={subject.name} path={subject.path} component={subject.component}/>
    })
  }
  render(){
    return (
      <div>
        <BrowserRouter>
          <Switch>
            <Login path="/login" />
            <Signup path="/signup" />
            <Route path="/" component={Home} exact/>
            {this.renderSubjectRoutes(SubjectRoutes)}
            <AddNote path="/addNote"/>
            <myProfile path={Meteor.userId()} /> //<-- Here
            <NotFound />
          </Switch>
        </BrowserRouter>
      </div>
    )
  }
}

Menu.js

import { Meteor } from "meteor/meteor"
import React from "react";
import { withRouter, Link } from "react-router-dom";

import { SubjectRoutes } from "./subjectRoutes/subjectRoutes";
import AddNote from "./AddNote";

class Menu extends React.Component{
  renderMenu(items){
    return items.map((item) => {
      return <p key={item.name}><Link to={item.path}>{item.name}</Link></p>
    })
  }
  render(){
    return(
      <div>
        <h1>Menu</h1>
        {this.renderMenu(SubjectRoutes)}
        <p><Link to="/addNote">Add a Note</Link></p>
        <p><Link to={Meteor.userId()}>My Profile</Link></p>
      </div>
    )
  }
}
export default withRouter(Menu)
like image 657
Stuart Fong Avatar asked Aug 22 '17 22:08

Stuart Fong


People also ask

Can we use nested routes in React?

js application to implement nested routing in the current version of react-router that is React Router DOM version 6. We use nested routing in our application so that a parent component has control over its child component at the route level. Prerequisites: The pre-requisites for this project are: React.

Can I pass Props to route component?

Here are few ways you can pass props to a route component. With the react-router v5, we can create routes by wrapping with a component, so that we can easily pass props to the desired component like this.

Does React Support route parameters?

Using React Router, when you want to create a Route that uses a URL parameter, you do so by including a : in front of the value you pass to Route 's path prop. Finally, to access the value of the URL parameter from inside of the component that is rendered by React Router, you can use React Router's useParams Hook.

How do I get the PATH variable in React Router?

To get path params in React Router, we can use the useParams hook. We create the Child component that calls the useParams hook to return an object with the route params in the URL. And we render the value of the id param on the page.


1 Answers

You are creating way more work for yourself, and this is the wrong way to add variables to route. What you're looking to do is add params to your route. In your case, you would want it to look something like this.

<Route path="/user/:userId" />

The : is what denotes that it is a parameter, ready to render a path based on the userId. So if you went to route /user/123 - it would be able to render user 123's data.

Here's some documentation to help you out.

https://reacttraining.com/react-router/web/example/url-params

like image 157
Christopher Messer Avatar answered Sep 29 '22 08:09

Christopher Messer