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

  • 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,
});
主站蜘蛛池模板: 西和县| 合肥市| 湖州市| 南木林县| 惠水县| 宾阳县| 万州区| 罗江县| 九江市| 福鼎市| 莱西市| 弥勒县| 灵璧县| 临汾市| 光泽县| 威远县| 鞍山市| 沅江市| 财经| 新营市| 资中县| 景泰县| 赣榆县| 射洪县| 应用必备| 大同市| 蓝田县| 尼勒克县| 建始县| 同德县| 涞水县| 兴城市| 左云县| 清河县| 通渭县| 谢通门县| 铁力市| 六安市| 北票市| 延庆县| 布尔津县|