React createRef yog dab tsi?
React createRef yog dab tsi?

Video: React createRef yog dab tsi?

Video: React createRef yog dab tsi?
Video: Build and Deploy a Modern React 18 Fitness Exercises App With APIs | RapidAPI 2024, Kaum ib hlis
Anonim

Thaum lub ref attribute yog siv rau ntawm ib qho HTML keeb, tus ref tsim nyob rau hauv lub constructor nrog Teb . tsimRef () tau txais DOM keeb kwm raws li nws cov khoom tam sim no. Thaum cov ref attribute yog siv rau ntawm cov kev cai hauv chav kawm ntawv, cov khoom ref tau txais qhov piv txwv ntawm cov khoom siv raws li nws qhov tam sim no.

Xav txog qhov no, dab tsi yog ref hauv ReactJS?

ReactJS | Refs . Refs yog ib txoj haujlwm muab los ntawm React kom nkag mus rau DOM lub caij thiab React lub caij uas koj yuav tau tsim ntawm koj tus kheej. Lawv yog siv nyob rau hauv cov ntaub ntawv uas peb xav hloov tus nqi ntawm ib tug me nyuam tivthaiv, tsis siv cov props thiab tag nrho cov.

Ib yam li ntawd, Forwardref yog dab tsi? Kaum ib hlis 9, 2019 6 mins nyeem. Ref forwarding nyob rau hauv Teb yog ib qho feature uas cia cov khoom xa mus ("forward") refs rau lawv cov me nyuam. Nws muab cov khoom siv rau menyuam yaus siv rau DOM cov khoom tsim los ntawm nws niam txiv cov khoom. Qhov no mam li tso cai rau tus menyuam nyeem thiab hloov cov ntsiab lus nyob qhov twg nws tau siv.

Ib yam li ntawd, koj tuaj yeem nug, koj siv ref react li cas?

Koj tuaj yeem tsim ib qho ref los ntawm kev hu Teb . createRef() thiab txuas a Teb element rau nws siv tus ref attribute ntawm lub caij. Peb tuaj yeem "xa mus" rau ntawm lub pob ref tsim nyob rau hauv txoj kev render nrog kev nkag mus rau tus cwj pwm tam sim no ntawm tus ref.

Yuav ua li cas koj hla ref rau cov ntsiab lus me nyuam?

Koj hla tus ref mus rau tus me nyuam tivthaiv raws li ib tug txawv lub npe prop - tiag tiag lub npe lwm yam tshaj li ref (e.g. buttonRef). Cov tus me nyuam tivthaiv Tom qab ntawd tuaj yeem xa tus prop mus rau DOM node ntawm tus ref tus cwj pwm. Qhov no cia niam txiv hla nws ref mus rau tus me nyuam DOM node los ntawm lub tiv thaiv nyob nruab nrab.

Pom zoo: