A cikin aikace-aikacen da aka yi amfani da Redux, gine-ginen ya ta'allaka ne akan manyan dabaru guda uku: Redux store, actions, da reducers. Bari mu zurfafa cikin kowane ɗayan waɗannan ra'ayoyin mu ga yadda suke aiki tare.
Redux Store
Redux store shine tushen gaskiya guda ɗaya wanda ke riƙe cikakken yanayin aikace-aikacen ku. Haƙiƙa abu ne na JavaScript wanda ke ƙunshe da bayanan da ke wakiltar duk yanayin aikace-aikacen. Kuna ƙirƙirar aikin store ta amfani da createStore
aikin daga ɗakin karatu na Redux.
Actions
Actions abubuwa ne a sarari JavaScript waɗanda ke bayyana wani abu da ya faru a cikin aikace-aikacen. Suna ɗaukar type
filin da ke nuna nau'in aikin da ake yi, kuma ana iya haɗa ƙarin bayanai kuma. Actions an ƙirƙira su ta amfani da masu ƙirƙira ayyuka, waɗanda ayyuka ne waɗanda ke mayar da abubuwan aiki. Misali:
// Action Types
const ADD_TODO = 'ADD_TODO';
// Action Creator
const addTodo =(text) => {
return {
type: ADD_TODO,
payload: text
};
};
Reducers
Reducers Ƙayyade yadda yanayin aikace-aikacen ke canzawa a mayar da martani ga actions. Mai ragewa aiki ne mai tsafta wanda ke daukar halin da ake ciki da kuma aiki a matsayin hujja kuma ya dawo da sabuwar jiha. Reducers ana haɗa su cikin tushen tushen guda ɗaya ta amfani da combineReducers
aikin. Ga misali mai sauƙi:
// Reducer
const todosReducer =(state = [], action) => {
switch(action.type) {
case ADD_TODO:
return [...state, action.payload];
default:
return state;
}
};
// Combine Reducers
const rootReducer = combineReducers({
todos: todosReducer,
// ...other reducers
});
Aiki Tare
Lokacin da kuka aika wani aiki ta amfani da dispatch
aikin, Redux yana tura aikin zuwa duk reducers. Kowane mai ragewa yana bincika idan nau'in aikin ya dace da nasa kuma yana sabunta sashin da ya dace na jihar daidai. Ana adana yanayin da aka sabunta a cikin Redux store, kuma duk wani haɗin da aka haɗa ya sake yin aiki bisa sabuwar jihar.
Misali Yanayi
Ka yi tunanin aikace-aikacen lissafin abin yi. Lokacin da mai amfani ya ƙara sabon abin todo, ana aika wani aiki tare da nau'in ADD_TODO
da rubutun todo azaman abin biya. Mai rage todos yana karɓar wannan aikin, yana ƙara sabon abin todo zuwa jihar, kuma ya dawo da yanayin da aka sabunta.
Kammalawa
Fahimtar yadda Redux store, actions, da reducers hulɗa yana da mahimmanci don ingantaccen gudanarwa na jiha. Wannan gine-gine yana tabbatar da rarrabuwar damuwa kuma yana sauƙaƙa sarrafa jihohin aikace-aikace masu rikitarwa. Yayin da kuke ci gaba da haɓakawa tare da Redux, waɗannan ra'ayoyin za su zama tushen dabarun gudanar da jihar ku.