Dab tsi yog qhov xwm txheej tshwm sim ( )?
Dab tsi yog qhov xwm txheej tshwm sim ( )?

Video: Dab tsi yog qhov xwm txheej tshwm sim ( )?

Video: Dab tsi yog qhov xwm txheej tshwm sim ( )?
Video: Xwm txheej tshwm sim txaus ntshai 7/23/2022 2024, Plaub Hlis Ntuj
Anonim

Txoj cai no siv tau thaum React hluavtaws xwm txheej yog siv nyob rau hauv lub asynchronous callback muaj nuj nqi tsis hu xwm txheej . nyob twj ywm (). React siv cov khoom SyntheticEvent los qhwv haiv neeg xwm txheej . Rau kev ua tau zoo yog vim li cas, hluavtaws xwm txheej tau sib sau ua ke thiab rov siv dua thoob plaws ntau haiv neeg xwm txheej.

Kuj nug, SyntheticEvent yog dab tsi?

Raws li official docs, SyntheticEvent yog tus ntoo khaub lig-browser wrapper ib ncig ntawm qhov browser qhov xwm txheej ib txwm muaj. Nws muaj qhov sib txuas zoo ib yam li qhov browser qhov xwm txheej ib txwm muaj, suav nrog stopPropagation() thiab tiv thaivDefault(), tshwj tsis yog cov xwm txheej ua haujlwm zoo ib yam thoob plaws txhua qhov browser.

Kuj Paub, qhov xwm txheej tiv thaivDefault () yog dab tsi? Teb siv hluavtaws xwm txheej tuav xwm txheej los ntawm khawm, tawm tswv yim thiab daim ntawv cov ntsiab lus. Nws qhia txog yuav ua li cas ntxiv ib yam khoom rau ib daim ntawv teev npe los ntawm kev siv ib daim ntawv nrog cov tswv yim thiab cov khoom khawm. Hauv qhov no, a tiv thaivDefault yog hu ua xwm txheej thaum xa daim foos los tiv thaiv qhov browser reload/refresh.

Ib yam li ntawd, cov xwm txheej React txawv ntawm cov xwm txheej DOM li niaj zaus li cas?

Siv React Event Handlers Qhov tseem ceeb sib txawv nruab nrab React qhov xwm txheej handlers thiab haiv neeg DOM kev tshwm sim handlers yog cov npe yog camelcase es tsis txhob me me, koj dhau hauv kev ua haujlwm es tsis txhob siv cov hlua, thiab koj yuav tsum tau hu xovtooj rau stopPropagation es tsis txhob rov qab tsis tseeb.

Kuv yuav ua li cas kom tsis txhob tshwm sim bubbling nyob rau hauv react?

Stop Event Bubbling : Yog koj xav tau nres tus kev tshwm sim bubbling , qhov no tuaj yeem ua tiav los ntawm kev siv cov xwm txheej . stopPropagation () txoj kev. Yog koj xav tau nres tus xwm txheej ntws los ntawm xwm txheej lub hom phiaj rau cov ntsiab lus saum toj kawg nkaus hauv DOM, xwm txheej . stopPropagation () txoj kev nres lub xwm txheej taug kev mus rau hauv qab mus rau saum.

Pom zoo: