Functional Components - Components Fudud oo Aan Lahayn State
React.js waa framework aad u xoog badan oo loogu talagalay horumarinta web-ka, gaar ahaan marka la sameynayo interfaces-ka isticmaale ee cad cad. Mid ka mid ah dhinacyada ugu muhiimsan ee React waa isticmaalka components. Qodobkan, waxaan diiradda saari doonaa nooca ugu fudud ee components ee React.js, kuwaas oo loo yaqaan Functional Components.
![]() |
Understanding Functional Components in React: Simple, Stateless Components |
Waa Maxay Functional Components?
Functional components waa kuwa sida ugu fudud loo sameeyo ee React.js. Waxaa lagu qoraa sida functions caadi ah oo JavaScript ah, waxayna soo celiyaan JSX (JavaScript XML) ama HTML code-ka loo baahan yahay in lagu soo bandhigo browser-ka. Functional components waxay ka duwan yihiin class components sababtoo ah ma lahan state, taas oo macnaheedu yahay inaysan xafidayn karin xaaladaha gudaha ee ay isticmaalaan.
Functional components waxaa loo yaqaanaa components aan lahayn state sababtoo ah uma baahna in ay keydiyaan xaalad ama data ay gudaha ku maamulaan. Tani waxay ka dhigeysaa kuwo aad u fudud oo waxqabadkooda wanaagsanaan kara sababtoo ah ma jiro state isbeddelaya oo ku kallifaya React inuu dib u cusbooneysiiyo (re-render). Taas bedelkeeda, waxay si joogto ah u soo celiyaan JSX ku saleysan xogta laga helo props, taas oo ku timaadda sidii input.
Tusaale Functional Component ah
Hoos waxaa ku yaal tusaale fudud oo functional component ah oo soo celiya salaam:
javascript
function SalaamComponent() {
return <h1>Salaan, Dunida!</h1>;
}
Tusaalaha kore, SalaamComponent
waa functional component aad u fudud oo soo celiya <h1>
element. Waa wax badan oo fudud oo cad, waana sababta ay functional components uga yihiin kuwa la doorbido marka laga hadlayo components aan u baahnayn xafididda state.
Haddaan qaadano tusaale kale oo yar oo loo adeegsaday props:
javascript
function SalaamComponent(props) {
return <h1>Salaan, {props.magac}!</h1>;
}
Halkan, SalaamComponent
wuxuu aqbalayaa props oo wuxuu soo bandhigaa magac kasta oo loo gudbiyo. Props waa hab fudud oo lagu gudbiyo xogta functional components si ay u noqdaan kuwa dib loo isticmaali karo.
Faa'iidooyinka Functional Components
Functional components waxay leeyihiin faa'iidooyin dhowr ah, kuwaas oo ka dhigaya kuwa aad loo isticmaalo, gaar ahaan xaaladaha aan u baahnayn state:
Fududeyn: Sababtoo ah functional components waa functions kaliya, waa sahlan yihiin in la fahmo, la qoro, lana tijaabiyo. Waxaa jira waxyaabo yar oo muhiim ah, taasoo ka dhigaysa in code-kaaga uu noqdo mid nadiif ah oo fudud.
Waxqabadka: Functional components waxay leeyihiin waxqabad wanaagsan sababtoo ah ma xafidaan state. Tani waxay yareyneysaa baahida loo qabo in React dib u qaabeeyo (re-render) markasta oo state isbedelo.
Isticmaalka Hooks: React 16.8 iyo wixii ka dambeeya, waxaad isticmaali kartaa Hooks, sida
useState
iyouseEffect
, oo waxaad ku dari kartaa sifooyinka components-kaaga adigoo isticmaalaya functional components. Tani waxay ka dhigeysaa functional components kuwo aad awood badan.Dib-u-adeegsiga iyo Qaybaha: Functional components waa la kala qaybin karaa oo dib loo isticmaali karaa si sahlan, taas oo ka dhigaysa kuwo aad waxtar u leh marka aad abuureyso interfaces isticmaale oo balaaran. Waxaad ku dari kartaa functional components dhowr meel oo kala duwan oo gudaha codsigaaga ah adigoo aan wax isbedel ah ku samaynin component-ka laftiisa.
Simplicity iyo Maintainability: Sababtoo ah functional components waxay ku saleysan yihiin functions fudud, way ka sahlan yihiin in la dayactiro oo la ballaariyo. Ma jiraan xaalado adag oo state ah oo la maareynayo, taasoo ka dhigaysa mid si sahlan loo fahmi karo marka kooxo badan ay ka shaqeynayaan hal mashruuc.
Gunaanad
Functional components waa hab sahlan oo awood leh oo lagu sameeyo components gudaha React.js. Waxay aad ugu habboon yihiin marka aad u baahantahay inaad abuurto components aan lahayn state, sida kuwa soo celiya macluumaad go'an oo browser-ka lagu soo bandhigo. Iyagoo si fiican ugu shaqeynaya waxqabad ahaan, isla markaana fududeynaya habka horumarinta, waa doorashada ugu fiican ee in badan oo React horumariyaasha ah.
Functional components waxay noqdeen xudunta horumarinta React sababtoo ah waxay fududeeyaan habka horumarinta iyagoo yareynaya baahida loo qabo complexity badan. Markaad fahanto sida ay u shaqeeyaan, waxaad awoodi doontaa inaad si sahlan u sameyso web applications adag oo leh code nadiif ah oo la fahmi karo. Functional components, oo isku darkooda la raaciyo awooda React Hooks, waxay si degdeg ah uga dhigayaan React.js mid ka mid ah kuwa ugu xooggan ee loogu talagalay horumarinta web applications-ka casriga ah.