Babban matsalar da ke da alaฦa da saman shafi na React Router shine cewa yana iya haifar da halayen da ba a zata ba yayin kewayawa tsakanin shafuka. Lokacin zagawa zuwa sabon shafi, mai binciken zai koma saman shafin, wanda zai iya zama mai ban tsoro ga masu amfani waษanda ke tsammanin zama a kan shafi ษaya ko gungura ฦasa gaba. Bugu da ฦari, wannan hali bazai iya tsammanin masu amfani waษanda aka yi amfani da su don ฦarin tsarin kewayawa yanar gizo na gargajiya ba.
import { useRouter } from 'react-router-dom'; const NextPage = () => { const router = useRouter(); const handleClick = () => { router.push('/next-page'); }; return ( <div> <button onClick={handleClick}>Go to next page</button> </div> ); };
Layin 1: Wannan layin yana shigo da ฦugiya mai amfani da mai amfani daga ษakin karatu na react-router-dom.
// Layin 3: Wannan layin yana bayyana wani aiki mai suna NextPage wanda ke dawo da bangaren React.
// Layin 4: Wannan layin yana bayyana maษalli da ake kira na'ura mai ba da hanya tsakanin hanyoyin sadarwa kuma ya sanya shi zuwa ฦugiya mai amfani.
Layin 6: Wannan layin yana bayyana wani aiki da ake kira handleClick wanda ke kiran hanyar turawa ta hanyar sadarwa tare da hujjar '/ shafi na gaba'.
Layi na 8-11: Waษannan layin suna dawo da bangaren React tare da maษalli mai maษalli wanda ke da saitin onClick don sarrafa Danna. Lokacin da aka danna, wannan zai kira aikin dannawa kuma kewaya zuwa '/shafi na gaba'.
Kewaya tsakanin shafuka
React Router babban ษakin karatu ne mai ba da hanya tsakanin hanyoyin sadarwa wanda aka gina a saman React wanda ke taimaka muku ฦara sabbin fuska kuma yana gudana zuwa aikace-aikacenku cikin sauri, duk yayin kiyaye URL ษin tare da abin da ake nunawa akan shafin. React Router yana sauฦaฦa kewayawa tsakanin shafuka a cikin aikace-aikacen React ta amfani da tsarin tushen sa. Tare da taimakon abubuwan haษin gwiwa kamar Link, NavLink, da Redirect, zaku iya ฦirฦirar hanyoyin kewayawa masu ฦarfi da mu'amala waษanda ke ba masu amfani damar kewaya app ษin ku ba tare da shigar da URLs da hannu ba. Bugu da ฦari, zaku iya amfani da abun tarihin da React Router ya bayar don kewaya cikin tsari tsakanin shafuka a cikin aikace-aikacenku.
GunguraToTop ko saman shafi na gaba
ScrollToTop siffa ce a cikin React Router wanda ke ba masu amfani damar gungurawa da sauri zuwa saman shafin yayin kewayawa tsakanin hanyoyi daban-daban. Yana da amfani musamman ga dogayen shafuka masu tarin abun ciki, saboda yana bawa masu amfani damar yin tsalle sama da sauri zuwa sama ba tare da gungurawa da hannu ba. Saman shafi na gaba wani nau'in fasalin ne wanda ke aiki iri ษaya amma maimakon gungurawa baya, yana ษaukar ku kai tsaye zuwa shafi na gaba lokacin kewayawa tsakanin hanyoyi. Wannan na iya zama taimako musamman ga masu amfani waษanda ke neman takamaiman bayani akan wani shafi kuma ba sa son yin gungurawa cikin duk abubuwan da ke wasu shafuka kafin isa wurin. Dukansu fasalulluka sune babban ฦari waษanda ke taimakawa haษaka ฦwarewar mai amfani da sauฦaฦe kewayawa da sauri.