Babban matsalar da ke da alaฦa da React Router DOM shine cewa yana iya zama da wahala a cire kuskure. Saboda React Router ne ke sarrafa hanyar, yana iya zama da wahala a iya gano ainihin inda batun ke faruwa. Bugu da ฦari, tun da React Router DOM yana amfani da JavaScript don sarrafa shi, duk wani kurakurai a cikin lambar na iya haifar da halin da ba zato ba tsammani kuma ya sa yin kuskure ya fi wahala. A ฦarshe, idan mai amfani yana da tsohuwar sigar React Router DOM da aka shigar, za su iya fuskantar matsalolin daidaitawa tare da sabbin nau'ikan ษakin karatu.
import { BrowserRouter as Router, Route } from "react-router-dom"; <Router> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> </Router>
1. "shigo da { BrowserRouter azaman na'ura mai ba da hanya tsakanin hanyoyin sadarwa, Hanyar } daga 'react-router-dom';"
Wannan layin yana shigo da kayan aikin BrowserRouter da Route daga ษakin karatu na amsa-router-dom.
2. "
Wannan layin yana ฦirฦirar ษangaren na'ura mai ba da hanya tsakanin hanyoyin sadarwa wanda za'a yi amfani da shi don naษe duk hanyoyin da ke cikin aikace-aikacen.
3. "
Wannan layin yana ฦirฦirar ษangaren Hanyar hanya wanda zai sanya bangaren Gida lokacin da hanyar ke '/'. The 'daidai' prop yana tabbatar da cewa wannan hanya za ta kasance daidai lokacin da hanyar ta kasance daidai'/'.
4. "
5. "" Wannan layin yana rufe ษangaren na'ura mai ba da hanya tsakanin hanyoyin sadarwa da kuma sigina don amsawa cewa an ayyana duk hanyoyin mu.
npm mai sarrafa kunshin
NPM (Node Package Manager) shine mai sarrafa fakitin JavaScript wanda ke taimaka wa masu haษakawa don shigarwa, ษaukakawa, da sarrafa fakiti don aikace-aikacen React cikin sauฦi. Shi ne tsoho mai sarrafa fakiti na ษakin karatu na React Router kuma yana ba da dama ga fakiti da yawa waษanda za a iya amfani da su a aikace-aikacen React. NPM yana ba masu haษakawa damar ganowa da shigar da fakiti cikin sauri daga wurin rajista na hukuma da kuma wasu kafofin ษangare na uku. Hakanan yana ba da kayan aiki don sarrafa abin dogaro tsakanin fakiti daban-daban, wanda ke sauฦaฦa lura da nau'ikan kowane fakitin a cikin aikace-aikacen. Bugu da ฦari, ana iya amfani da NPM don sabunta fakitin da ke cikin sauฦi ko ma cire su idan ba a buฦatar su.
Menene React Router dom
React Router DOM babban ษakin karatu ne don React wanda ke ba masu haษakawa damar ฦirฦira da sarrafa hanyoyin cikin aikace-aikacen React ษin su. Yana ba da hanya don bayyana taswirar hanyoyin zuwa abubuwan da aka gyara, sarrafa tarihin mai binciken, da kuma kiyaye UI cikin aiki tare da URL. Hakanan ya haษa da fasali kamar daidaitawar hanya mai ฦarfi, sarrafa canjin wuri, da tsara URL.
Yadda shigar Dom npm react router
1. Sanya React Router:
Da farko, shigar da kunshin React Router ta amfani da npm ko yarn.
Misali, idan kuna amfani da npm:
npm shigar react-router-dom
2. Shigo React Router:
Da zarar an gama shigarwa, kuna buฦatar shigo da abubuwan haษin daga react-router-dom cikin aikace-aikacen ku. Misali:
shigo da { BrowserRouter azaman mai ba da hanya tsakanin hanyoyin sadarwa, Hanyar } daga 'react-router-dom';
3. Kunna App ษin ku a cikin Na'ura mai ba da hanya tsakanin hanyoyin sadarwa:
Mataki na gaba shine kunsa sashin tushen ku tare da a
const App = () => (
);
4. ฦara Hanyoyi zuwa App ษin ku: Mataki na ฦarshe shine ฦara hanyoyin zuwa aikace-aikacenku ta amfani da
const App = () => (
)