Home » Create React Nested Router component

Create React Nested Router component

Today, We want to share with you Create React Nested Router component.In this post we will show you Nested routes with react router v4 / v5, hear for Simple Nested Routes with Layouts in React Router we will give you demo and example for implement.In this post, we will learn about Create Nested Tab Routes with Dynamic Paths with React-Router with an example.

Create React Nested Router component

There are the Following The simple About Create React Nested Router component Full Information With Example and source code.

Read Also:  php search engine script for mysql database

As I will cover this Post with live Working example to develop Nested Routes – React router beginners guide, so the Understanding Nested Routing in React Directory structures for this example is following below.

React Rendering Our List

./src/containers/ProductsPage.js

import React from 'react';
import { connect } from 'react-redux';
import ProductsList from '../components/ProductsList';

const ProductsPage = ({ products }) => 
  <div>
    <ProductsList products={products} />
  </div>;

const mapStateToProps = (state) => {
  return {
    products: state.products
  };
}

export default connect(mapStateToProps)(ProductsPage);

./src/components/ProductsList.js

import React from 'react';
import { Link } from 'react-router-dom';

const ProductsList = ({ products }) => {
  const renderProducts = products.map(product => 
    <Link key={product.id} to={`/products/${product.id}`}>{product.title}</Link>
  );

  return (
    <div>
      {renderProducts}
    </div>
  );
};

export default ProductsList;

./src/containers/ProductsShow.js

import React from 'react';

const ProductsShow = props => {

  return (
    <div>
      <h3>Products Show Component!</h3>
    </div>
  );
}

export default ProductsShow;

.src/containers/ProductsPage.js

import React from 'react';
import { Route } from 'react-router-dom';
import { connect } from 'react-redux';
import ProductsList from '../components/ProductsList';
import ProductShow from './ProductShow';

const ProductsPage = ({ match, products }) => 
  <div>
    <ProductsList products={products} />
    <Route path={`${match.url}/:productId`} component={ProductShow}/>
    <Route exact path={match.url} render={() => (
      <h3>Please select a Product from the list.</h3>
    )}/>
  </div>;

const mapStateToProps = (state) => {
  return {
    products: state.products
  };
}

export default connect(mapStateToProps)(ProductsPage);

./src/components/ProductsList.js

import React from 'react';
import { Link } from 'react-router-dom';

const ProductsList = ({ products }) => {
  const renderProducts = products.map(product => 
    <Link to={`/products/${product.id}`}>{product.title}</Link>
  );

  return (
    <div>
      {renderProducts}
    </div>
  );
};

export default ProductsList;

Web Programming Tutorials Example with Demo

Read :

Read Also:  Count total number of items in nested array in PHP

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Create React Nested Router component.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Replicate Copy Table Row Examples
  2. Read Also:  get_post_meta in WordPress Example
  3. Read Also:  File upload and sending data to backend using angular js
  4. Read Also:  Laravel 5.8 Get Client Ip Address
  5. Read Also:  Number Filter using Angular Example
  6. Read Also:  Nested routing in React Router
  7. Read Also:  Laravel MySQL Database Relationship Queries
  8. Read Also:  PHP Config Class to store configuration data
  9. Read Also:  Laravel Application Directory Structure Example
  10. Read Also:  Laravel 5.8 Get Client Ip Address

Leave a Comment

Your email address will not be published. Required fields are marked *