最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use React Router with match.params? - Stack Overflow

programmeradmin8浏览0评论

i'm doing the project and i need to create some routes with React Router. My project is:

Each square has a id 200and 201, and each turn that i to click i want to go for route like: http://localhost:3000/user/200 or http://localhost:3000/user/201 and when to go for that route, i want it to appear in the body "User 200" or "User201", i read the documentation but did not understand..

My APP.JS:

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import './App.css';
import Home from './Home';

const App = () => (
  <Router>
    <Home/>
  </Router>
);


export default App;

i'm doing the project and i need to create some routes with React Router. My project is:

Each square has a id 200and 201, and each turn that i to click i want to go for route like: http://localhost:3000/user/200 or http://localhost:3000/user/201 and when to go for that route, i want it to appear in the body "User 200" or "User201", i read the documentation but did not understand..

My APP.JS:

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import './App.css';
import Home from './Home';

const App = () => (
  <Router>
    <Home/>
  </Router>
);


export default App;

My HOME.JS:

import React from 'react';
import Header from './Header';
import Body from './Body';
import './Home.css';

class Home extends React.Component {
  render() {
    return ( <
      div className = 'home' >
      <
      Header / >
      <
      Body / >
      <
      /div>
    )
  }
}

export default Home;

My BODY.JS:

import React from 'react';
import './Body.css';
import axios from 'axios';
import {
  Link
} from "react-router-dom";

class Body extends React.Component {
  constructor() {
    super();

    this.state = {
      employee: [],
    }
  }

  ponentDidMount() {
    axios.get('http://127.0.0.1:3004/employee').then(
      response => this.setState({
        employee: response.data
      })
    )
  }

  getName = () => {
    const {
      employee
    } = this.state;
    return employee.map(name => < Link className = 'link'
      to = '/user' > < div key = {
        name.id
      }
      className = 'item' > < img className = 'img'
      src = {
        `https://picsum.photos/${name.name}`
      } > < /img> <h1 className='name'> {name.name} </h
      1 > < /div> </Link > )
  }

  render() {
    return ( <
      div className = 'body' > {
        this.getName()
      } <
      /div>
    )
  }
}


export default Body;

Someone would can help me please??

Share Improve this question asked Apr 28, 2018 at 18:26 JotaJota 8656 gold badges25 silver badges42 bronze badges 1
  • Hi @Jota , did my answer solve your problem? – Milos Mosovsky Commented May 2, 2018 at 18:17
Add a ment  | 

2 Answers 2

Reset to default 11

At first you didn't setup your router correctly

App.js

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'

class App extends React.Component {
  render () {
    return (
      <Router>
        <Home>
          <Switch>
            <Route path='/user/:token' ponent={Body} />
          </Switch>
        </Home>
      </Router>
    )
  }
}

Home.js - now you are wrapping router inside children

class Home extends React.Component {
  render () {
    return (<div className='home' >
      <Header />
      {this.props.children} {/* This line will render router children which will be Body */}
    </div>
    )
  }
}

Body.js

Inside body you will now receive this.props.match.params I think you will figure out by yourself

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'

class Approute extends React.Component {
  render () {
    return (
      <Router>
         <Home>
           <Switch>
             <Route path='/user/:token' ponent={Body} />
           </Switch>
        </Home>
      </Router>
    )
  }
}

发布评论

评论列表(0)

  1. 暂无评论