Learn React Router v6
Introduction
React Router is used to make the dynamic Single Page Applications (SPA) without affecting the user interface with the reloads and can give the multi page functionality through the navigation in react
Outlet
It is used to render the child components where all the child components shared the same parent component example navigation bar.
First Step is to add the special key name children and provide the array of object route definitions whose route , will start from the parent route. It is the special route which will have the component that contains the outlet from the react-router and the component which will render in all child components. In this example , we have the Root layout component which will be rendered to home child component for the "/" route
Second step is to create the special component that will contain the common element and the outlet component from the react-router . In this example , we have the main navigation bar which will be shared to home child component.
NavLinks
Navlinks is used to see which link is currently active when we click on the link it will get highlighted , so the user is able to look at the currently active page . It has the active property in className and style where we can use the active property and style accordingly.
Links
Links used to write down the path in the search bar with the help of "to="/" prop . It listens the click event and switch with the mentioned path so that the react router then navigate and render the component.
Link redirect the user to the path and it appears as the anchor tag . <a> tag in the html reloads the page whenever some user clicks on the tag but link disabled the reloading and enhance the user interaction to the website.
Use Params are used to add dynamic path . Whenever the path cannot be hardcoded and content creates with the user interaction , in this case we create the dynamic path.
This starts with the colon and some variable ( /:id ) whose value will be accessed to component whose path is dynamic . Parameters will be accessed by importing useParams hook and accessing the properties of parameters. To specify the URL parameters is optional , append the question mark at the end :id?
Navigate Programmatically
At first we used to navigate with links , but we can also navigate dynamically with the useNavigate hook by extracting the function from the useNavigate and specifying the path in the first parameter . In this example , when onClick function will triggered , this navigate to the path "/events" . So that's how we can move to any path by useNavigate hook.
Absolute & Relative Path
When path followed by the "/" it will start from the starting and will not append after the path which is called absolute path
For example :- "/home" path will not append to any path because it starts with slash
When path doesn't contain "/" it will append to the path . For example "edit" :- it will append to the path from where we navigate this path.
<Link to=".."> Back </Link> ( This will only work with relative paths which will back to page from where it calls )
We can also specify the prop of relative ,having two options when it sets to "path" , (..) will go back to the previously visited path and default it set to the route , which navigates to the parent route.
So, this is some basic concepts of react router which is pretty useful and important when using navigation in react app.
Thank you for reading :)
Comments
Post a Comment