An warware: React na'ura mai ba da hanya tsakanin hanyoyin sadarwa dom npm

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. "Wannan layin yana haifar da bangaren Route wanda zai sanya About bangaren lokacin da hanyar ke '/about'.

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 bangaren daga react-router-dom. Wannan zai samar da aikace-aikacen ku tare da damar sarrafa bayanai kuma ya sa shi sane da hanyar URL na yanzu wanda mai amfani ke ziyarta. Misali:

const App = () => (
 
 

  {/* Hanyoyi zuwa nan */}
 

    );

4. ฦ˜ara Hanyoyi zuwa App ษ—in ku: Mataki na ฦ™arshe shine ฦ™ara hanyoyin zuwa aikace-aikacenku ta amfani da bangaren samar da react na'ura mai ba da hanya tsakanin hanyoyin sadarwa dom. Bangaren hanya yana ษ—aukar abubuwa biyu; hanya da bangaren da ke ba ka damar tantance abubuwan da ya kamata a yi lokacin da mai amfani ya ziyarci wata hanyar URL a cikin aikace-aikacenka Misali:

const App = () => (
 
 

          // yana sanya Maษ“allin Gida lokacin da mai amfani ya ziyarci hanyar โ€œ/โ€ url                  // yana fassara Game da Bangaren lokacin da mai amfani ya ziyarci hanyar โ€œ/game daโ€ url       

   )

Shafi posts:

Leave a Comment