官术网_书友最值得收藏!

  • 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,
});
主站蜘蛛池模板: 广丰县| 孟州市| 安顺市| 汉阴县| 安阳县| 泽普县| 耿马| 宜丰县| 罗定市| 红桥区| 永新县| 新郑市| 博罗县| 三原县| 屏东市| 阳曲县| 浙江省| 淮安市| 黑河市| 黄山市| 桐柏县| 区。| 腾冲县| 安宁市| 元阳县| 乌鲁木齐县| 驻马店市| 邹城市| 小金县| 西华县| 彰武县| 鹿泉市| 永善县| 逊克县| 临沧市| 浙江省| 平阴县| 三原县| 通化市| 堆龙德庆县| 西平县|