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.