freeCodeCamp/guide/english/certifications/front-end-libraries/redux/use-a-switch-statement-to-h.../index.md

59 lines
1.3 KiB
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: Use a Switch Statement to Handle Multiple Actions
---
## Use a Switch Statement to Handle Multiple Actions
Tip: Make sure you don't use "break" commands after return statements within the switch cases.
### Hint 1
Specific actions will be passed into the reducer function. Look at the action creator functions (e.g. loginUser) to see what values you will need to check for in your switch case statements.
2018-10-12 19:37:13 +00:00
### Hint 2
Each case condition should return an updated authenticated property object.
2018-10-12 19:37:13 +00:00
### Hint 3
Do not forget to include a default case in your statement which returns the defaultState.
### Solution
```javascript
const defaultState = {
authenticated: false
};
const authReducer = (state = defaultState, action) => {
// change code below this line
switch(action.type){
case 'LOGIN':
return {
authenticated: true
};
case 'LOGOUT':
return {
authenticated: false
};
default:
return defaultState;
}
// change code above this line
};
const store = Redux.createStore(authReducer);
const loginUser = () => {
return {
type: 'LOGIN'
}
};
const logoutUser = () => {
return {
type: 'LOGOUT'
}
};
```
2018-10-12 19:37:13 +00:00
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->