Vim li cas peb thiaj siv REF IN react?
Vim li cas peb thiaj siv REF IN react?

Video: Vim li cas peb thiaj siv REF IN react?

Video: Vim li cas peb thiaj siv REF IN react?
Video: KYC PI Network ăn chắc 100% 2024, Hlis ntuj nqeg
Anonim

Refs yog muaj nuj nqi muab los ntawm Teb nkag mus rau DOM lub ntsiab lus thiab cov Teb lub ntsiab ntawd koj tej zaum yuav tau tsim ntawm koj tus kheej. Lawv siv hauv rooj plaub qhov twg peb xav hloov tus nqi ntawm tus me nyuam tivthaiv, tsis ua siv ntawm cov khoom siv thiab tag nrho.

Cov lus nug tseem yog, koj siv REF IN react JS 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.

Ib sab saum toj no, koj ua li cas dhau refs hauv kev xav? Peb txhais a ref hauv cov khoom uas xav tau ref thiab hla nws mus rau lub khawm tivthaiv. Teb yuav hla tus ref los ntawm thiab xa nws mus rau <button ref ={ ref }> los ntawm kev qhia nws ua JSX tus cwj pwm. Thaum lub ref yog txuas, ref . tam sim no yuav taw tes rau DOM node.

Tsis tas li ntawd, react createRef ua dab tsi?

Tsim Refs - Siv Teb . tsimRef () thiab muab lawv rau Teb cov ntsiab lus ntawm ref attribute. Qhov tseem ceeb, koj muab cov Ref rov qab los ntawm Teb . tsimRef () mus rau ib qho khoom vaj khoom tsev, thaum ib qho khoom siv yog tsim (aka, nyob rau hauv lub ntsiab constructor). Li no, tus Ref ua tau raug xa mus thoob plaws hauv cov khoom.

ref hauv HTML yog dab tsi?

Cov ref attribute ua rau nws muaj peev xwm khaws tau ib qho kev siv rau ib qho React element lossis cov khoom xa rov qab los ntawm cov khoom siv render() configuration function. Qhov no tuaj yeem muaj txiaj ntsig thaum koj xav tau ib qho kev siv, los ntawm hauv ib qho kev tivthaiv, mus rau qee lub ntsiab lus lossis cov khoom siv uas muaj nyob rau hauv lub render() muaj nuj nqi.

Pom zoo: