If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In "React Advanced - Class 2," we'll continue exploring more advanced React concepts and build on what we learned in the previous class. We'll cover topics such as React Router advanced features, lazy loading, and code splitting.
Step 1: React Router - Code Splitting with React.lazy and Suspense
React.lazy is a function that allows you to dynamically load a component. It helps with code splitting by only loading the component when it's needed.
jsxCopy code
import React, { Suspense } from 'react'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; // Lazy load components const Home = React.lazy(() => import('./Home')); const About = React.lazy(() => import('./About')); function App() { return ( <Router> <div> <h1>Hello, React!</h1> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> </ul> </nav> <Suspense fallback={<div>Loading...</div>}> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> </Suspense> </div> </Router> ); } export default App;
In this example, the
Home
andAbout
components are lazily loaded usingReact.lazy
and wrapped in aSuspense
component. TheSuspense
component displays the fallback content (e.g., "Loading...") while the lazy-loaded components are being loaded.Step 2: Nested Routes with React Router
You can create nested routes by defining additional
Route
components inside a component.jsxCopy code
import React, { Suspense } from 'react'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; const Home = React.lazy(() => import('./Home')); const About = React.lazy(() => import('./About')); const Products = React.lazy(() => import('./Products')); function App() { return ( <Router> <div> <h1>Hello, React!</h1> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> <Link to="/products">Products</Link> </li> </ul> </nav> <Suspense fallback={<div>Loading...</div>}> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> <Route path="/products" component={Products} /> </Suspense> </div> </Router> ); } export default App;
In this example, we added a new component
Products
and nested it within theSuspense
component.Step 3: Redirecting with React Router
React Router allows you to perform redirects with the
Redirect
component or thehistory
object.jsxCopy code
import React, { Suspense } from 'react'; import { BrowserRouter as Router, Route, Link, Redirect } from 'react-router-dom'; const Home = React.lazy(() => import('./Home')); const About = React.lazy(() => import('./About')); const Products = React.lazy(() => import('./Products')); function App() { return ( <Router> <div> <h1>Hello, React!</h1> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> <Link to="/products">Products</Link> </li> </ul> </nav> <Suspense fallback={<div>Loading...</div>}> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> <Route path="/products" component={Products} /> {/* Redirect from /home to / */} <Redirect from="/home" to="/" /> </Suspense> </div> </Router> ); } export default App;
In this example, we added a redirect from
/home
to/
using theRedirect
component. Now, if the user visits/home
, they will be redirected to the home page (/
).Step 4: React Router - URL Parameters
React Router allows you to capture URL parameters and pass them as props to the rendered component.
jsxCopy code
import React, { Suspense } from 'react'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; const UserProfile = React.lazy(() => import('./UserProfile')); function App() { return ( <Router> <div> <h1>Hello, React!</h1> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/user/123">User Profile</Link> </li> </ul> </nav> <Suspense fallback={<div>Loading...</div>}> <Route exact path="/" component={Home} /> <Route path="/user/:userId" component={UserProfile} /> </Suspense> </div> </Router> ); } export default App;
In this example, we added a route with a parameter
:userId
. The value of the parameter in the URL will be passed as theuserId
prop to theUserProfile
component.Step 5: Using React Router - Location and History
React Router provides access to the current
location
andhistory
objects using theuseLocation
anduseHistory
hooks.jsxCopy code
import React, { Suspense } from 'react'; import { BrowserRouter as Router, Route, Link, useLocation, useHistory } from 'react-router-dom'; const UserProfile = React.lazy(() => import('./UserProfile')); function App() { const location = useLocation(); const history = useHistory(); const handleGoBack = () => { history.goBack(); }; return ( <Router> <div> <h1>Hello, React!</h1> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/user/123">User Profile</Link> </li> </ul> </nav> <Suspense fallback={<div>Loading...</div>}> <Route exact path="/" component={Home} /> <Route path="/user/:userId" component={UserProfile} /> </Suspense> </div> </Router> ); } export default App;
In this example, we use the
useLocation
anduseHistory
hooks to get the current location and history objects. We also added ahandleGoBack
function that useshistory.goBack()
to navigate back to the previous page.Congratulations! You've completed the second class of React Advanced. You've learned about code splitting with React.lazy and Suspense, nested routes, redirects, handling URL parameters, and using location and history objects with React Router.
React and React Router offer powerful tools to build complex and performant applications. As you continue your React journey, you can explore more advanced topics like server-side rendering (SSR) with React, React context, error boundaries, and using advanced hooks like
useReducer
anduseRef
.Keep practicing, building more complex applications, and exploring the vast possibilities of React and React Router! Happy coding!
Comments: 24
Kindly check the lecture number 6. Lifecycle of a Change in 1. Git and Github The above lecture is not related to the course please update it ASAP Thank you
From where can I download the files which love babbar says he has uploaded on dashboard ??
I have attached the link https://github.com/lakshayk12/ANN_optimization_BTP
bro, i paid for the course, but still can't access the course. do something
Paid for the course but it still locked, can solve this problem please, when you take the payment the couse should be unlocked, evey time i have to message for any purchase
Now your Paypal payment is accepted and your course is activated successfully. Please leave your valuable feedback.
Why i am being asked for payment if i am already enrolled in the course
Dear anand, your payment is now updated check your course, apologize for several delays. Please leave your valuable feedback.
thanks its working now
hello ,I have a doubt
hello i have a doubt
hi
I'm paying through my Card, but it giving an error. why ? how we can purchase a course in Pakistan. Paypal is banned here
can we download the videos
i done my payment for mern stack development love babbar but cant acces to course kindly give me access
Dear Krishnapal your payment is already accepted go and check back.
Why i am being asked for payment if i am already enrolled in the course
Dear Shivansh, if you already done your payment then contact us on our official Email id [email protected] or reply here.
Why i am being asked for payment if i am already enrolled in the course
Dear p8354046, if you already done your payment then contact us on our official Email id [email protected] or reply here.
Why i am being asked for payment if i am already enrolled in the course
Dear, Don't worry now your problem is solved check your id.
Why i am being asked for payment if i am already enrolled in the course
Yes bro
I'm facing same issue
Dear, Don't worry now your problem is solved check your id.
i want debit card option
how can i assure that this is not scam?
i have same doubt is this website real or a fraud
No its not any fraud. its genuine if you need any proof please contact us via email.
No it's not any scam, its genuine if you need any proof please contact us via email.
how to do payment ?
Click on Watch now button then click next video then payment option is showing
i am unable to access the video even i have already completed my payment .
Your problem is solved
plz add codes lecture wise ! it wold be great and helpful also
now we have added starter package in lecture 2nd, go now and download it.
how to view this one
Why video not play
Now it's working.
Thank you for the access of the videos. It would be greatful if I could no the way to download the videos
Use idm to download
ok
Hello bro can you share course with me?
hello everyone