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

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,
});
主站蜘蛛池模板: 汉中市| 正镶白旗| 霍城县| 南投市| 崇州市| 福安市| 镇平县| 钦州市| 弥渡县| 德庆县| 河源市| 伊川县| 沛县| 张北县| 定日县| 南宁市| 瑞丽市| 南投县| 沈阳市| 贡山| 张家口市| 河曲县| 驻马店市| 永定县| 芦山县| 西宁市| 龙江县| 渭源县| 盐池县| 万州区| 陆丰市| 衡山县| 无极县| 久治县| 松阳县| 广水市| 中山市| 钦州市| 宣威市| 永修县| 榆林市|