Kuv puas yuav tsum siv cov refs hauv kev xav?
Kuv puas yuav tsum siv cov refs hauv kev xav?

Video: Kuv puas yuav tsum siv cov refs hauv kev xav?

Video: Kuv puas yuav tsum siv cov refs hauv kev xav?
Video: puas yog koj ntshai yuav kuv ntsaim vaj 2022 -2023 2024, Hlis ntuj nqeg
Anonim

Thaum koj ua tau add ib ref rau cov menyuam yaus, qhov no tsis yog qhov kev daws teeb meem zoo tshaj plaws, vim tias koj tsuas yog tau txais ib qho piv txwv es tsis yog DOM node. Tsis tas li ntawd, qhov no yuav tsis ua haujlwm nrog cov khoom siv ua haujlwm. Yog koj siv React 16.3 lossis siab dua, peb xav kom siv ref xa mus rau cov xwm txheej no.

Hauv qhov no, vim li cas peb thiaj siv refs hauv kev xav?

Refs yog ib txoj haujlwm 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 yog 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.

Tsis tas li ntawd, ua li cas koj hla refs nyob rau hauv react? 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, cov refs puas muaj kev cuam tshuam tsis zoo?

Raws li official teb docs, Txawm tias txoj hlua refs tsis yog deprecated, lawv suav hais tias legacy, thiab tej zaum yuav deprecated nyob rau hauv tej lub sij hawm nyob rau hauv lub neej yav tom ntej.

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.

Pom zoo: