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

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,
});
主站蜘蛛池模板: 大港区| 宁晋县| 丘北县| 德昌县| 富裕县| 凤阳县| 读书| 洱源县| 台安县| 锡林郭勒盟| 巴林左旗| 金川县| 开鲁县| 高要市| 隆子县| 佛坪县| 秦安县| 睢宁县| 溆浦县| 云和县| 旌德县| 福鼎市| 衡东县| 县级市| 天台县| 林口县| 怀集县| 肇庆市| 怀远县| 青冈县| 南通市| 淮阳县| 庆安县| 沙雅县| 诏安县| 延边| 绥阳县| 甘孜县| 吴桥县| 台中市| 广元市|