Tabbas! Anan ga hanyar shigar da 'react-router-dom' a cikin app.
React Router DOM an san shi da kasancewa kayan aiki mai ฦarfi da ฦayyadaddun kayan aiki don aikace-aikacen da aka yi tare da `React.js`. Babban manufarsa ita ce aiki tare da mahaษin mai amfani tare da kowane canje-canje a cikin URL ษin mai lilo. Lokacin canzawa zuwa ra'ayoyi daban-daban, zaku iya zaษar don guje wa sake loda dukkan shafin, wanda ke ba aikace-aikacen gidan yanar gizon ku ฦarin ruwa da saurin mai amfani.
//Install React Router DOM npm install react-router-dom
Magani tare da React Router DOM
Don bincika amfanin wannan ษakin karatu, bari mu yi la'akari da aikace-aikacen zance tare da saukowa, game da shafukan tuntuษar. Muna nufin kewaya cikin waษannan shafuka ba tare da buฦatar sabunta shafin yanar gizon duk lokacin da aka danna maษallin kewayawa ba.
Don cim ma wannan, mun aiwatar da 'react-router-dom' a cikin aikace-aikacen mu na React. Wannan ya ฦunshi matakai na gaba ษaya:
1. Shigar da ษakin karatu ta amfani da npm.
2. Shigo abubuwan da suka dace daga ษakin karatu cikin aikace-aikacenku.
3. Aiwatar da na'ura mai ba da hanya tsakanin hanyoyin sadarwa.
Saita mataki-mataki da aiwatarwa
Bari mu karya kowane mataki ฦasa don ku sami ฦarin haske game da yadda ake haษa 'react-router-dom' cikin ayyukanku.
Da farko, gudanar da umarni mai zuwa a cikin na'ura wasan bidiyo don ฦara 'react-router-dom' a cikin abubuwan dogaronku:
npm install react-router-dom
Na gaba, kuna buฦatar shigo da BrowserRouter, road, Da kuma link abubuwan da aka gyara daga 'react-router-dom' zuwa babban bangaren aikace-aikacenku (yawanci 'App.js').
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
Bayan haka, kunsa babban bangaren tare da bangaren 'Router'. Duk wani abu a cikin 'Router' zai sami damar zuwa React Router kuma zai iya amfani da hanyar sadarwa.
<Router> //The rest of the application comes here </Router>
Daga baya, zaku iya saita hanyoyi don ra'ayoyinku daban-daban ta amfani da bangaren 'Hanyar hanya'. Ace kana da 'HomePage', 'AboutPage', da 'ContactPage', hanyar za ta kasance kamar haka:
<Route path="/" exact component={HomePage} /> <Route path="/about" component={AboutPage} /> <Route path="/contact" component={ContactPage} />
Kuma a ฦarshe, kuna iya amfani da abubuwan haษin 'Link' a cikin mashigin kewayawa inda kuke son sanya hanyoyin kewayawa:
<Link to="/">Home</Link> <Link to="/about">About</Link> <Link to="/contact">Contact</Link>
Waษannan su ne mahimman matakan yin amfani da `react-router-dom` don yin tuฦi.
ฦarin ษakunan karatu da Ayyuka
'React Router DOM' cikakke ne, amma kuna iya buฦatar ฦarin ayyuka don ฦara sigogi masu ฦarfi zuwa URL, sarrafa shafuka 404, hanyoyin gida, ko ma canza hanyoyin da tsari. Bincika fasaloli kamar su `Switch`, `Redirect`, `useParams`, da `useHistory` don samun duk wani tsari mai ma'ana kan tukwici a aikace-aikacen ku.
A ฦarshe, `react-router-dom' shine mafita mai ฦarfi don aikace-aikacen shafi ษaya da aka yi a cikin `React.js`. Aiwatar da shi yana da sauฦi kuma abubuwan amfaninsa na iya zama masu fa'ida sosai ga aikin gidan yanar gizon da ฦwarewar mai amfani.