Categories

Tuesday, April 23, 2024
#919814419350 therichposts@gmail.com
Bootstrap 5Reactjs

How to get optional parameter value from url in reactjs?

How to get optional parameter value from url in reactjs?

Hello friends, welcome back to my blog. Today in this blog post, I am going to tell you, How to get optional parameter value from url in reactjs?

Guys with post we will cover below things:

  1. React routing.
  2. React routing with multiple parameters.
  3. Get parameter value in reactjs.
  4. For better working experience please check the below video.
Working Video
How to get optional parameter value from url in reactjs?
How to get optional parameter value from url in reactjs?

For react js new comers, please check the below link:
React js Basic Tutorials


Friends now I proceed onwards and here is the working code snippet and please use this carefully to avoid the mistakes:

1. Firstly friends we need fresh reactjs setup and for that we need to run below commands into our terminal and also w should have latest node version installed on our system:

Guys you can skip this first step if you already have reactjs fresh setup:

npx create-react-app reactdemo

cd reactdemo

npm start // run the project

2. Now friends, we need to run below commands into our reactjs project to install bootstrap(for good looks), react router for dynamic routing modules:

Guys you can skip this first step if you already have these modules:

npm install bootstrap --save

npm install --save react-router-dom

npm start

3. Now friends, we need to create new Home.js file inside src folder and add below code in that file:

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
class Home extends React.Component {
  
render() {
  return (
    <div className="p-3 mb-2 bg-secondary text-white">
      <h1>Home Component</h1>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    </div>
  );
  
}

}
export default Home;

4. Friends now, we need to create new About.js file inside src folder and add below code in that file:

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
class About extends React.Component {
    componentDidMount()
    {
      console.log(this.props.match.params.pathParam2) // see the value in console
    }
render() {
  return (
    <div className="p-3 mb-2 bg-primary text-white">
      <h1>About Component</h1>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    </div>
  );
  
}

}
export default About;

5. Finally friends we need to add below code into our src/App.js file to get final output on web browser:

import React from 'react';

import './App.css';
//Calling Bootstrap styles
import 'bootstrap/dist/css/bootstrap.min.css';

//Routing
import Home from './Home';
import About from './About';

//Router Modules Router for routing, Switch for switching components related to routes, Link for routes linking
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';


class App extends React.Component {
  
render() {
  return (
    <Router>
      <nav class="navbar navbar-expand-sm bg-light">
        <ul class="navbar-nav">
          <li class="nav-item">
            <Link class="nav-link" to={''}>Home</Link>
          </li>
          <li class="nav-item">
            <Link class="nav-link" to={'/about/test1/test2'}>About</Link>
          </li>
     
        </ul>
      </nav>
      
      <div className="container p-5">
        <Switch>
          <Route exact path='/' component={Home} />
          <Route exact path='/home' component={Home} />
          <Route exact path='/about/:pathParam1?/:pathParam2?' component={About} />
        </Switch>
      </div>
  </Router>
  );
  
}

}
export default App;

Now we are done friends. If you have any kind of query or suggestion or any requirement then feel free to comment below.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. For better understanding must watch video above.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.