Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Commit

Permalink
Allow incrementAsync to take an argument and update layout
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLorantfy committed Mar 9, 2020
1 parent d71bc4d commit 08fc6ee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 1 addition & 3 deletions template/src/features/counter/Counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ export function Counter() {
>
Add Amount
</button>
</div>
<div className={styles.row}>
<button
className={styles.asyncButton}
onClick={() => dispatch(incrementAsync())}
onClick={() => dispatch(incrementAsync(Number(incrementAmount) || 0))}
>
Add Async
</button>
Expand Down
1 change: 1 addition & 0 deletions template/src/features/counter/Counter.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
.asyncButton {
composes: button;
position: relative;
margin-left: 8px;
}

.asyncButton:after {
Expand Down
18 changes: 11 additions & 7 deletions template/src/features/counter/counterSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ export const slice = createSlice({
},
});

// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state) => state.counter.value)`
export const selectCount = state => state.counter.value;

export const { increment, decrement, incrementByAmount } = slice.actions;

export const incrementAsync = () => dispatch => {
// The function below is called a thunk and allows us to perform async logic. It
// can be dispatched like a regular action: `dispatch(incrementAsync(10))`. This
// will call the thunk with the `dispatch` function as the first argument. Async
// code can then be executed and other actions can be dispatched
export const incrementAsync = amount => dispatch => {
setTimeout(() => {
dispatch(increment());
dispatch(incrementByAmount(amount));
}, 1000);
};

// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state) => state.counter.value)`
export const selectCount = state => state.counter.value;

export default slice.reducer;

0 comments on commit 08fc6ee

Please sign in to comment.