- Redux Quick Start Guide
- James Lee Tao Wei Suresh Kumar Mukhiya
- 224字
- 2021-07-02 12:40:30
Action creators
JavaScript functions that take some arguments and return actions are action creators. Let's look at an action creator function for adding a new doctor to the application:
function addNewDoctor(data) {
return {
type: ADD_NEW_DOCTOR_REQUEST,
data
};
}
Now, you can think of a function that you might need for deleting a record, as follows:
function deleteDoctor(identifier) {
return {
type: "DELETE_DOCTOR_REQUEST",
identifier
};
}
Before we move on to reducers, let's make one more action creator for authentication. Generally, to authenticate, we use an email and password. So, in order to authenticate (or deauthenticate) we need to define actions. Please note that the actions that we define will be used in our project for a hospital management system. Our action for authentication could look something like the following:
export const authenticate = (credentials) => ({
type: "AUTHENTICATE",
payload: credentials
});
export const deauthenticate = () => ({
type: "DEAUTHENTICATE"
});
Similarly, let's create action creators for registering a user. When we register a user, we are likely to have a request, a success, or a failure. Based on these three states, we can create the action creators, as follows:
export const onRegisterRequest = user => ({ type: REGISTER_REQUEST, user });
export const onRegisterSuccess = user => ({ type: REGISTER_SUCCESS, user });
export const onRegisterFailure = message => ({
type: REGISTER_FAILURE,
message,
});
- JavaScript從入門(mén)到精通(微視頻精編版)
- DevOps with Kubernetes
- PaaS程序設(shè)計(jì)
- MATLAB圖像處理超級(jí)學(xué)習(xí)手冊(cè)
- Mastering QGIS
- Python測(cè)試開(kāi)發(fā)入門(mén)與實(shí)踐
- Mastering Kotlin
- 64位匯編語(yǔ)言的編程藝術(shù)
- RISC-V體系結(jié)構(gòu)編程與實(shí)踐(第2版)
- ASP.NET開(kāi)發(fā)與應(yīng)用教程
- Solr Cookbook(Third Edition)
- 細(xì)說(shuō)Python編程:從入門(mén)到科學(xué)計(jì)算
- 零基礎(chǔ)學(xué)Java第2版
- 實(shí)驗(yàn)編程:PsychoPy從入門(mén)到精通
- C++服務(wù)器開(kāi)發(fā)精髓