An warware: Yi amfani da Tarihi React Router v6 app

Babban matsalar da ke da alaฦ™a da amfani da History React Router v6 shine cewa baya goyan bayan tuฦ™i na tushen zanta. Wannan yana nufin cewa duk URLs dole ne su zama cikakkun hanyoyi, wanda zai iya yin wahalar sarrafawa da kiyaye aikace-aikacen. Bugu da ฦ™ari, babu wani ginanniyar tallafi don hanyoyi masu ฦ™arfi, wanda zai iya zama matsala lokacin ฦ™irฦ™irar ฦ™aฦ™ฦ™arfan aikace-aikace tare da shafuka masu yawa. A ฦ™arshe, Tarihi React Router v6 baya bayar da kowane tallafi don ma'anar sabar-gefen sabar, wanda zai iya zama dole a wasu lokuta.

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

 function App() {

   const history = useHistory();

   // Handle a button click to push a new entry onto the history stack.
   function handleClick() {
     history.push("/new-location");
   }

   return (
     <div>      
       <button type="button" onClick={handleClick}>Go to New Location</button>       

       <Switch>        
         <Route path="/new-location">          
           <NewLocation />        
         </Route>      

       </Switch>    

     </div>   ); }

// Wannan lambar tana shigo da BrowserRouter, Sauyawa, Hanya, da amfani da abubuwan Tarihi daga ษ—akin karatu na amsa-router-dom.
// Sannan yana ฦ™irฦ™irar wani aiki mai suna App wanda ke amfani da ฦ™ugiya ta tarihi don ฦ™irฦ™irar abun tarihi.
// Ana amfani da wannan abu na tarihi a cikin wani aiki da ake kira handleClick wanda ke tura sabon shigarwa akan tarihin tarihin lokacin da aka kira shi.
// Ayyukan App sai ya dawo da wasu JSX wanda ya haษ—a da maษ“alli tare da mai kula da onClick wanda ke kira handleClick lokacin da aka danna.
// A ฦ™arshe, akwai wani ษ“angaren Sauyawa tare da bangaren Route guda ษ—aya a cikinsa wanda ke sanya bangaren NewLocation lokacin da hanyarsa ta dace da "/ sabon wuri".

Menene tarihin amfani

UseHistory wani React Hook ne wanda React Router ke bayarwa wanda ke ba da damar abubuwan haษ—in gwiwa don samun damar abun tarihin don kewaya ta tsari. Ana iya amfani da shi don tura sabbin wurare zuwa tarin tarihi, maye gurbin wurin da ake ciki, komawa baya a tarihi, da sauransu.

Ta yaya zan sami tarihin hanya a cikin martani

A cikin React Router, zaku iya samun tarihin hanya ta amfani da ฦ™ugiya ta amfani da Tarihi. Wannan ฦ™ugiya tana ba da damar yin amfani da misalin tarihin da za ku iya amfani da shi don kewayawa, komowa da gaba a cikin tarihin app ษ—in ku, da ฦ™ari. Don amfani da shi, kawai shigo da ฦ™ugiya daga React Router sannan ku kira shi a cikin sashin ku:

shigo da {amfaniHistory} daga 'react-router-dom';

const MyComponent = () => {
tarihin tarihi = tarihin amfani ();

// Yanzu zaku iya samun damar tarihin hanya ta hanyar abu 'tarihi'.

dawo (...);
}

Yana amsa mai amfani da na'ura mai ba da hanya tsakanin hanyoyin sadarwa API

Ee, React Router yana amfani da HTML5 History API don kiyaye yanayin wurin da ake ciki da tarihinsa. Wannan yana ba da damar React Router don sabunta shafin ba tare da sake loda shi ba, yin kewayawa cikin sauri da sauฦ™i. API ษ—in Tarihi kuma yana ba da damar haษ—in kai mai zurfi, wanda ke sauฦ™aฦ™a wa masu amfani don raba hanyoyin haษ—in kai waษ—anda ke kai su kai tsaye zuwa takamaiman shafi a cikin aikace-aikacen.

Shafi posts:

Leave a Comment