Babban matsalar da ke da alaฦa da turawa a cikin React Router v6 shine cewa tsarin haษin kai ya canza sosai daga nau'ikan da suka gabata. A cikin v6, dole ne a yi amfani da bangaren Redirect maimakon
In React Router v6, you can use the <Redirect> component to redirect from one page to another. Example: import { Redirect } from 'react-router-dom'; <Route exact path="/old-path"> <Redirect to="/new-path" /> </Route>
1. shigo da {Komawa} daga 'react-router-dom';
- Wannan layin yana shigo da bangaren Redirect daga ษakin karatu na react-router-dom.
2.
- Wannan layin yana haifar da ษangaren Hanya tare da ainihin hanyar "/ tsohuwar hanya".
3.
- Wannan layin yana amfani da bangaren Redirect don turawa daga "/ tsohuwar-hanyar" zuwa "/ sabuwar-hanyar".
Ta yaya zan iya turawa a cikin React Router v6
v6
React Router v6 yana ba da sashin Juya kai wanda za'a iya amfani dashi don tura masu amfani daga shafi ษaya zuwa wani. Don amfani da bangaren Redirect, kuna buฦatar shigo da shi daga fakitin react-router-dom. Bangaren turawa yana ษaukar abubuwa biyu: daga kuma zuwa. The "daga" prop shine hanyar shafin na yanzu, kuma "zuwa" prop shine hanyar shafin da kake son tura masu amfani zuwa. Misali, idan kuna son tura masu amfani daga /shafin gida zuwa /game da su, lambar ku zata yi kama da haka:
shigo da {Komawa} daga 'react-router-dom';
Menene React Router?
React Router babban ษakin karatu ne don React wanda ke ba masu haษakawa damar ฦirฦirar aikace-aikacen shafi guda ษaya tare da kewayawa da ฦarfi, tsarin jigila. Yana taimakawa don kiyaye UI a daidaita tare da URL, yana sauฦaฦa wa masu amfani don rabawa da alamar URLs. React na'ura mai ba da hanya tsakanin hanyoyin sadarwa kuma yana ba da fasaloli masu ฦarfi kamar ษora nauyi, kariyar hanya, da sarrafa canjin wuri.
Nau'in na'ura mai ba da hanya tsakanin hanyoyin sadarwa
React Router babban ษakin karatu ne don React wanda ke ba masu haษaka damar ฦirฦirar aikace-aikacen shafi guda ษaya tare da kewayawa da hanyar URL. Yana ba da nau'ikan hanyoyin sadarwa iri uku: BrowserRouter, HashRouter, da MemoryRouter.
BrowserRouter: Wannan na'ura mai ba da hanya tsakanin hanyoyin sadarwa tana amfani da tarihin HTML5 API don kiyaye UI ษin ku tare da URL. Ana amfani dashi lokacin da kake son amfani da URL na ainihi a cikin aikace-aikacen ku.
HashRouter: Wannan na'ura mai ba da hanya tsakanin hanyoyin sadarwa tana amfani da sashin hash na URL (watau #) don kiyaye UI ษin ku tare da URL. Ana amfani da shi lokacin da ba kwa son amfani da URL na ainihi ko kuma lokacin da kuke buฦatar dacewa tare da tsofaffin masu bincike waษanda basa goyan bayan tarihin HTML5 API.
MemoryRouter: Wannan na'ura mai ba da hanya tsakanin hanyoyin sadarwa yana adana tarihin wurare a cikin ฦwaฦwalwar ajiya kuma baya yin hulษa tare da ma'aunin adireshin mai lilo ko ฦirฦirar URLs na gaske. Yana da amfani don dalilai na gwaji ko don wuraren da ba a so yin amfani da URL na ainihi (misali, ma'anar ษangaren uwar garken).