2

//ArticlePage

const ArticlePage = ({ match }) => {
    const name = match.params.name;
    return (
    <>
        <h1>
            This is {name} Article
        </h1>
    </>
);
    }
export default ArticlePage;

//App

import { BrowserRouter, Route, Routes } from 'react-router-dom';
import NavBar from "./NavBar";
import NotFound from "./pages/NotFound";
import ArticlePage from "./pages/ArticlePage";

function App () {
  return (
    <div className="App">
      <BrowserRouter>
        <NavBar />
        <Routes> 
          <Route exact path='/' element ={<HomePage />} />
          <Route path = '/topic' element = {<Topic />} />
          <Route path = '/about' element = {<AboutPage />} />
          <Route path = '/article/:name' element = {<ArticlePage />} /> 
          <Route path = '*' element = {<NotFound />} />
        </Routes>
      </BrowserRouter>
    </div>
   );
}

export default App;

//NavBar Page

import { Link } from 'react-router-dom';
import './NavBar.css';

const NavBar = () => (
    <nav>
        <ul>
            <li className='li'>
                <Link to ="/" className='link' > Home </Link>
            </li>
            <li className='li'>
                <Link to ="/about" className='link' > About </Link>
            </li>
            <li className='li'>
                <Link to = "/article" className='link'> Article </Link>
            </li>
            <li className='li'>
                <Link to = "/topic" className='link' > Topic </Link> 
            </li>
        </ul>
    </nav>
);

export default NavBar;

enter image description here

**I am trying to pass URL parameter using variable 'name' but soon as i access ArticlePage I get 404:Not Found (content of my NotFound Page) .I'm trying to get value of 'name' variable using props (match) in ArticlePage but I'm unable to render it. Fairly new to React, any hint or solution would be appreciated. I'm using React v6. **

8
  • Link's to attribute should have "/article/some_name" I guess. Also, you should use useParams to get the URL parameters. Commented Dec 31, 2021 at 9:50
  • That's a fairly different thing here 'name' refer to variable. As far as i learnt variable is passed like that! Commented Dec 31, 2021 at 9:53
  • I meant Link in your Navbar not the Route. Like this <Link to = "/article/some_name" className='link'> Article </Link> Commented Dec 31, 2021 at 9:53
  • That doesn't even make sense article here link to one of my Article Page. I have issue with URL parameter. If someone type '/article' he should refer to my article main-page but if someone type '/article/learn-react' then i want to refer him to that page using URL parameter. Commented Dec 31, 2021 at 9:58
  • Then you should probably add a separate route for that. Commented Dec 31, 2021 at 10:00

1 Answer 1

2

Try this code it's works

import { useParams } from 'react-router-dom';

const ArticlePage = () => {
   const params = useParams();
  return (
    <>
      <h1>
        This is {params.name} Article
      </h1>
    </>
  );
}
export default ArticlePage;
Sign up to request clarification or add additional context in comments.

4 Comments

Is it necessary to pass match as parameter while using useParams.
No need pass match in parameter ! i update my answer
Can you refer any article about usage of match and useParams beacuse I'm bit confused though it solves my problem.

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.