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

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.

Shafi posts:

Leave a Comment