Tiešā thunk sūtīšana Redux
Kad mēs nosūtījām action no komponentēm, tad
izmantojām hook useDispatch, kurš
atgriež store metodi dispatch, jo
komponentēm nav tiešas piekļuves store.
Šoreiz mums ir jāiegūst objekti
ar pārdevējiem un jādara tas vienreiz, kad
tiek palaists mūsu lietotne. Mēs to varam
izdarīt tieši no galvenā faila
main.jsx lietotnes, pēc tam, kad
workers sāks darboties.
Atvērsim mūsu lietotni ar
produktiem, un tajā failu main.jsx un
importēsim tajā thunk fetchSellers:
import { fetchSellers } from './parts/sellers/sellersSlice'
Šajā failā mums ir pieejams store,
tāpēc šeit nav nepieciešami nekādi hook un
mēs varam tieši izmantot
tā metodi dispatch. Pievienosim pēc
rindiņas:
await worker.start({ onUnhandledRequest: 'bypass' })
Šādu:
store.dispatch(fetchSellers())
Palaism mūsu lietotni. Tagad produktu kartītēs norādīti arī pārdevēji:
Atveriet savu lietotni ar studentiem.
Atveriet tajā failu main.jsx. Importējiet
tajā savu thunk fetchTeachers un nosūtiet
to tieši ar metodi store.dispatch.