2017-08-22 8 views
0

私は作成しているアプリケーションを持っていて、どのように変数を挿入するのだろうと思っています<Route path={insert variable here} component={myProfile}> myProfileページを作成しようとしています。それをhttp://mywebsite.com/userIdにリダイレクトしますが、path引数に変数Routeを作成しようとすると、そのパスでレンダリングしようとしているコンポーネントが返されません。React - 経路パスに変数を挿入する

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) 

答えて

1

あなたは自分の道より多くの仕事を作成しているが、これはルートに変数を追加するには間違った方法です。あなたが探しているのは、paramsをルートに追加することです。あなたの場合、あなたはこのようなものを見たいでしょう。

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

:userIdに基づいてパスをレンダリングする準備ができて、それがパラメータであることを意味するものです。したがって、ルート/user/123に行くと、ユーザー123のデータを表示できます。

ここにいくつかのドキュメントがあります。

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

+0

だから私はMeteor.userIdではuserIdに代わる()???申し訳ありませんが、私は流星にはかなり新しく反応します –

関連する問題