Yuav ua li cas koj nres cov khoom los ntawm unmounting?
Yuav ua li cas koj nres cov khoom los ntawm unmounting?

Video: Yuav ua li cas koj nres cov khoom los ntawm unmounting?

Video: Yuav ua li cas koj nres cov khoom los ntawm unmounting?
Video: yuav coj li cas kom luag nyiam 2024, Kaum ib hlis
Anonim

Siv teb -router koj tuaj yeem yooj yim tiv thaiv hloov txoj kev (uas yuav tiv thaiv tiv thaiv tshem tawm ) los ntawm kev siv Prompt. Koj yuav tsum tau manually hla tus getUserConfirmation prop uas yog ib qho haujlwm. Koj tuaj yeem hloov kho qhov haujlwm no raws li koj nyiam nyob rau hauv ib qho Router (Browser, Memory lossis Hash) los tsim koj cov lus pom zoo kev cai (xws li.

Kuj paub yog, ua li cas koj unmount lub feem ntawm ib tug react?

Teb. Yog lawm, ReactDOM muab txoj hauv kev los tshem tawm ib qho tiv thaiv los ntawm DOM los ntawm txoj cai manually. Koj tuaj yeem siv txoj kev ReactDOM. unmountComponentAtNode(container), uas yuav tshem tawm ib qho mounted React tivthaiv los ntawm DOM nyob rau hauv lub thawv uas tau teev tseg, thiab ntxuav ib qho ntawm nws cov xwm txheej tuav thiab xeev.

Tsis tas li ntawd, hu tsis tau rau setState lossis forceUpdate ntawm ib qho kev tivthaiv unmounted Qhov no tsis muaj op? Ceeb toom: Tsis tuaj yeem hu rau setState (lossis forceUpdate) ntawm cov khoom siv tsis tau txuas . Qhov no yog ib tsis muaj - op , tab sis nws qhia txog kev nco hauv koj daim ntawv thov. Txhawm rau txhim kho, tshem tawm tag nrho cov npe thiab cov haujlwm asynchronous hauv cov txheej txheemWillUnmount. Yog li kev daws teeb meem tib neeg feem ntau tuaj txog yog siv Redux.

Nyob rau hauv txoj kev no, dab tsi yog ib qho unmounted tivthaiv?

Hu rau setState() hauv ib qho unmounted tivthaiv txhais tau tias cov tiv thaiv tsis raug ntxuav kom zoo ua ntej ua unmounted , uas yog, koj lub app tseem muaj kev siv rau ib qho unmounted tivthaiv.

Thaum twg kuv yuav tsum siv componentWillUnmount?

componentWillUnmount yog qhov kawg ua haujlwm raug hu tam sim ua ntej cov khoom raug tshem tawm ntawm DOM. Nws feem ntau yog siv los ua kom huv si rau ib qho DOM-cov ntsiab lus lossis cov sijhawm ua haujlwm tsim hauv componentWillMount. Hauv picnic, componentWillUnmount sib raug rau ua ntej koj tuaj tos koj daim pam picnic.

Pom zoo: