An warware: amsa na'ura mai ba da hanya tsakanin hanyoyin sadarwa ta amfani da sifofi na tsaye

Babban matsalar da ke da alaฦ™a da amfani da tsayayyen salo tare da React Router shine cewa yana iya zama da wahala a kiyaye hanyoyin hanyoyi daban-daban da salon haษ—in su. Tare da sifofi na tsaye, kowace hanya tana buฦ™atar samun tsarinta na dokokin CSS, waษ—anda zasu iya zama marasa ฦ™arfi da wahala da sauri. Bugu da ฦ™ari, idan an yi amfani da salo a hanyoyi da yawa, yana buฦ™atar a kwafi shi a cikin duka, yana da wahala a kiyaye lambar DRY (Kada ku Maimaita Kanku).

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import styled from 'styled-components';
 
const StyledLink = styled(Link)`
  color: palevioletred;
  font-weight: bold;

  &:hover {
    color: white;
    text-decoration: none;  
  }  
`;

 const App = () => (   <Router>     <div>       <ul>         <li><StyledLink to="/">Home</StyledLink></li>         <li><StyledLink to="/about">About</StyledLink></li>       </ul>       <hr />       <Route exact path="/" component={Home} />       <Route path="/about" component={About} />     </div>   </Router>) ;

 const Home = () => (   <div>     <h2>Home</h2>   </div>) ;

 const About = () => (   <div>     <h2>About</h2>   </div>) ;

 export default App;

1. Layin farko yana shigo da ษ—akin karatu na React.
2. Layi na biyu yana shigo da kayan aikin BrowserRouter, Route, da Link daga ษ—akin karatu na react-router-dom.
3. Layi na uku yana shigo da kayan da aka tsara daga ษ—akin karatu mai salo.
4. Layi na huษ—u yana ฦ™irฦ™irar ษ“angaren StyledLink ta amfani da ษ“angaren Link da aka shigo da shi daga react-router-dom kuma ya sanya shi da launi: palevioletred da font-weight: m, da kuma tasirin hover wanda ke canza launinsa zuwa fari kuma yana cire kowane rubutu. ado idan an shata.
5. Layi na biyar ya samar da wani nau'in App wanda ke mayar da bangaren na'ura mai ba da hanya tsakanin hanyoyin sadarwa (Router components) da ke cikinsa (daya na Gida da na About). Hakanan yana ba da jerin hanyoyin haษ—in yanar gizo guda biyu marasa tsari (Gida da Game da) ta amfani da ษ“angaren StyledLink da aka ฦ™irฦ™ira a Layi 4 da ke sama su duka biyun sun rabu da alamar hr don dalilai na salo.
6. Layi na 8 - 11 sun ฦ™irฦ™iri wasu sassa guda biyu masu aiki da ake kira Gida da Game da wannan suna sanya h2 tags tare da sunayensu daban-daban a cikin su lokacin da aka kira su ta hanyar hanyoyin hanyoyin su a cikin layi na 5 a sama da su duka biyu (Gida yana fassara "Gida" yayin da Game da ke fassara "Game da" ).
7. A ฦ™arshe, Line 12 yana fitar da bangaren App don haka ana iya amfani da shi a wani wuri a cikin aikace-aikacen mu idan an buฦ™ata

A tsaye Salon

Salon madaidaici a cikin React Router salo ne da aka yi amfani da su akan wani sashi kuma suna zama iri ษ—aya ba tare da la'akari da kowane canje-canje ga yanayin ษ“angaren ko kayan aikin ba. Wannan ya bambanta da salo mai tsauri, waษ—anda ke canzawa dangane da yanayi ko kayan aikin wani sashi. Za a iya amfani da salo na tsaye don ฦ™irฦ™irar daidaiton kamanni da jin daษ—in aikace-aikacen, da kuma samar da hanya mai sauฦ™i ga masu haษ“akawa don saurin salon abubuwan da aka gyara ba tare da daidaita su da hannu a duk lokacin da aka samu canji ba.

kunshin sassa masu salo

Abubuwan Salon Salon sanannen fakitin React Router ne wanda ke ba masu haษ“akawa damar ฦ™irฦ™ira da sarrafa salon matakin-bangare a cikin aikace-aikacensu na React. Yana ba da hanya mai sauฦ™i don rubuta lambar CSS wacce aka keษ“ance zuwa sassa guda ษ—aya, yana sauฦ™aฦ™a don kiyayewa da cirewa. Salo-bangaren kuma yana sauฦ™aฦ™a raba salo a cikin sassa da yawa, da kuma bayar da goyan baya don jigo. Bugu da ฦ™ari, ana iya amfani da abubuwan da aka tsara tare da React Router's Link bangaren, kyale masu haษ“akawa su tsara hanyoyin haษ—in yanar gizo a cikin aikace-aikacen su.

Yadda ake amfani da Static Styles

Za a iya amfani da salo na tsaye a cikin React Router ta amfani da sifa na salon layi. Wannan sifa tana ba ku damar amfani da salo kai tsaye zuwa wani abu ba tare da buฦ™atar takardar salo daban ba. Don amfani da tsayayyen salo a cikin React Router, kuna buฦ™atar ฦ™irฦ™irar abu mai salo sannan ku wuce shi azaman hujja ga salon kayan aikin. Misali:

const myStyle = {
bangon launi: '#f2f2f2',
Girman font: '20px',
launi: '#000'
};

Shafi posts:

Leave a Comment