Qhov sib npaug ntawm CheebtsamDidMount hauv hooks yog tus useEffect muaj nuj nqi. Kev ua haujlwm dhau mus rau useEffect raug tua rau txhua qhov kev ua haujlwm-tshwj tsis yog tias koj dhau qhov kev sib cav thib ob rau nws.
Yuav siv li cas rau?
Ua li cas thiaj siv tau? Los ntawm kev siv Hook no, koj qhia React tias koj cov khoom yuav tsum ua qee yam tom qab ua tiav. React yuav nco ntsoov cov haujlwm uas koj dhau mus (peb yuav xa mus rau nws raws li peb "txhim kho"), thiab hu rau nws tom qab ua tiav DOM hloov tshiab.
Puas yog tivthaivDidMount ib React nuv?
useEffect yog React nuv qhov twg koj tuaj yeem siv cov kev mob tshwm sim, piv txwv li, tau txais cov ntaub ntawv los ntawm server. Thawj qhov kev sib cav yog hu rov qab uas yuav raug rho tawm haujlwm tom qab browser layout thiab xim.
Vim li cas sivEffect tsis hu?
Yog vim li cas qhov no tshwm sim yog vim qhov kev hu api yog asynchronus, nws tsis populate lub xeev tam sim no, yog li qhov kev tshwm sim thawj zaug thiab sim nyeem. tam sim no los ntawm thawj huab cua xeev null. Kev daws: hauv koj txoj kev render, xyuas kom tsis txhob nyeem huab cua.
Puas sivEffect ib txwm khiav ntawm Mount?
Ib qho tseem ceeb: tus siv cov txiaj ntsig yuav ib txwm khiav ntawm mount tsis hais txogyog tias muaj dab tsi hauv nws qhov kev cia siab. Tej zaum peb tsis xav ua qhov cuam tshuam rau peb cov ntaub ntawv thaum nws tsis tau txhais (raws li nws yuav nyob rau hauv thawj render) tab sis peb xav tos kom txog thaum nws muaj neeg nyob ntawm API hu.