Skip to content

Commit 0db79e8

Browse files
author
Catharina Mesquita
committed
v2 - com duck pattern
1 parent 2b3cf34 commit 0db79e8

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/components/ComponentList/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { useSelector, useDispatch } from 'react-redux';
3+
import { Types } from '../../store/reducer';
34

45
export default function ComponentList() {
56

@@ -11,7 +12,7 @@ export default function ComponentList() {
1112
const dispatch = useDispatch();
1213

1314
function add(){
14-
dispatch({ type: 'ADD_REDUCER', title: 'GrafQL'})
15+
dispatch({ type: Types.ADD, title: 'GrafQL'})
1516
}
1617

1718
return (

src/store/index.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
import { createStore } from 'redux'
22

3-
const INITIAL_STATE = {
4-
data: [
5-
'React Native',
6-
'ReactJS',
7-
'NodeJS'
8-
]
9-
}
10-
11-
function reducer(state = INITIAL_STATE, action){
12-
switch (action.type) {
13-
case 'ADD_REDUCER':
14-
return { ...state, data: [ ...state.data, action.title ]}
15-
default:
16-
return state;
17-
}
18-
}
3+
import reducer from './reducer'
194

205
const store = createStore(reducer)
216

src/store/reducer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createActions, createReducer } from 'reduxsauce';
2+
3+
export const { Types, Creators } = createActions({
4+
add: ["title"]
5+
})
6+
7+
const INITIAL_STATE = {
8+
data: [
9+
'React Native',
10+
'ReactJS',
11+
'NodeJS'
12+
]
13+
}
14+
15+
const add = (state = INITIAL_STATE, action) => {
16+
return { ...state, data: [ ...state.data, action.title ]}
17+
}
18+
19+
export default createReducer(INITIAL_STATE, {
20+
[Types.ADD]: add,
21+
})

0 commit comments

Comments
 (0)