An Warware: Yadda ake turawa a cikin React Router v6

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 element, kuma dole ne a samar da abin da za a yi amfani da shi tare da wani abu mai ษ—auke da kaddarorin suna. Bugu da ฦ™ari, duk wani ฦ™arin kayan aiki kamar sigogin jiha ko tambaya dole kuma a haษ—a su cikin wannan abun. Wannan na iya sa ya zama mai wahala ga masu haษ“akawa waษ—anda aka saba amfani da su don yin amfani da mafi sauฦ™i na juzu'in React Router na farko.

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).

Shafi posts:

Leave a Comment