An warware: React router yana ƙara koma baya don kama duka

Babban matsalar da ke da alaƙa da React Router da ƙara koma baya don kama duka shine yana iya zama da wahala a daidaita hanyar dawo da kyau yadda yakamata. Ana buƙatar daidaita hanyar dawowa ta yadda za ta kama duk buƙatun, gami da waɗanda ba su da ingantattun hanyoyin. Idan tsarin ba a yi daidai ba, to buƙatun hanyoyin da ba daidai ba ba za a kama su ta hanyar koma baya ba kuma yana iya haifar da kurakurai ko halayen da ba a zata ba. Bugu da ƙari, idan aikace-aikacen ya ƙunshi hanyoyi masu ƙarfi (misali, dangane da shigarwar mai amfani), to waɗannan suna buƙatar la'akari da su lokacin da za a daidaita hanyar dawowa ta yadda ita ma ta kama su.

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

const App = () => (
  <Router>
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />

      {/* Fallback route */}
      <Route component={NoMatch} /> 

    </Switch>
  </Router>  
);

Layin 1: Wannan layin yana shigo da BrowserRouter, Route, da Canja abubuwan da aka gyara daga ɗakin karatu na react-router-dom.
// Layin 2: Wannan layin yana bayyana ma'anar ma'anar da ake kira App wanda shine bangaren aiki.
Layin 3: Wannan layin yana mayar da bangaren na'ura mai ba da hanya tsakanin hanyoyin sadarwa daga react-router-dom.
Layin 4: Wannan layin yana mayar da bangaren Sauyawa daga react-router-dom.
Layi na 5 & 6: Waɗannan layukan suna samar da abubuwan haɗin Hanyar hanya guda biyu tare da ainihin hanyoyi da abubuwan haɗin da za a yi lokacin da waɗannan hanyoyin suka dace.
// Layin 8: Wannan layin yana ba da hanyar komawa baya idan babu ɗayan hanyoyin da aka daidaita. Zai sanya bangaren NoMatch idan babu wasu hanyoyin da suka dace.

Menene React Router

React Router babban ɗakin karatu ne don aikace-aikacen React. Yana ba masu haɓaka damar ƙirƙirar hanyoyi da abubuwan haɗin da za a iya amfani da su don kewaya tsakanin shafuka daban-daban a cikin aikace-aikacen React. Hakanan yana ba da fasali kamar daidaitawar hanya mai ƙarfi, sigogin tambaya, da yanayin wuri. Bugu da ƙari, yana ba da goyan baya don ma'anar sabar-gefen uwar garke da rarrabuwar lamba.

Kama-duk hanyar koma baya

Hanyar koma baya hanya ce a cikin React Router wacce ta dace da kowace hanyar da ba ta dace da kowace hanyar ba. Ana amfani da irin wannan nau'in hanya sau da yawa don ƙirƙirar shafi 404, ko don samar da wani sashi na duk hanyoyin da ba su dace ba. Yana da mahimmanci a lura cewa hanyar kama-dukkan koma baya yakamata koyaushe ta kasance hanya ta ƙarshe a cikin jerin hanyoyin, saboda zai dace da kowace hanya kuma yana hana sauran hanyoyin daidaitawa.

Yadda ake ayyana hanyar koma baya da kyau

Lokacin amfani da React Router, hanyar komawa baya hanya ce da ake amfani da ita lokacin da babu wasu hanyoyin da suka dace da URL ɗin da ake nema. Yawancin lokaci ana amfani da shi don tura masu amfani zuwa shafi na 404 ko wani shafi lokacin da URL ɗin da ake nema bai wanzu ba.

Don ayyana hanyar koma baya da kyau a cikin React Router, yakamata ku fara ƙirƙirar a bangaren kuma kunsa shi kewaye da hanyoyinku. Ciki cikin bangaren, yakamata ku hada da hanyoyinku na yau da kullun da a bangaren ba tare da kayyade hanya ba. Wannan zai zama hanyar koma baya kuma zai kama duk wani buƙatun da bai dace da kowane ɗayan hanyoyinku ba. Hakanan zaka iya ƙayyade abin da ya kamata ya faru idan wannan hanya ta dace, kamar turawa zuwa shafi 404 ko nuna wani abun ciki.

Me yasa hanyar koma baya ta kasance koyaushe ta fara tashi

Hanyar komawa baya a cikin React Router koyaushe ana haifar da shi lokacin da hanyar URL ba ta dace da kowace hanyoyin da ake da su ba. Wannan na iya faruwa lokacin da mai amfani ya rubuta da hannu a cikin URL ɗin da ba daidai ba, ko kuma idan ba a daidaita dabarun sarrafa aikace-aikacen da kyau ba. Hanyar koma baya tana ba masu haɓaka damar gudanar da waɗannan al'amuran cikin alheri da ba da amsa ga mai amfani, kamar shafi 404 ko tura su zuwa shafin gida.

Shafi posts:

Leave a Comment