Skip to content

Commit 5f214bc

Browse files
committed
Implementando função de deletar
1 parent 7beaaf0 commit 5f214bc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/components/ItemList/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import React from 'react';
2-
2+
import { useDispatch } from 'react-redux';
3+
import { Types } from '../../store/reducer/example-reducer';
34
// import { Container } from './styles';
45

56
export default function ItemList({ item }) {
7+
8+
const dispatch = useDispatch();
9+
10+
function remove() {
11+
let id = item.id
12+
dispatch({ type: Types.REMOVE, id })
13+
}
14+
615
return (
716
<span>
817
{item.text} - {item.createAt}
18+
<button type="button" onClick={remove}>DELETE</button>
919
</span>
1020
);
1121
}

src/store/reducer/example-reducer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { createActions, createReducer } from 'reduxsauce';
55
* Ex.: Type.ADD e add = { type: Type.ADD, params }
66
*/
77
export const { Types, Creators } = createActions({
8-
add: ["note"]
8+
add: ["note"],
9+
remove: ["id"]
910
})
1011

1112
const INITIAL_STATE = {
@@ -16,6 +17,10 @@ const add = (state = INITIAL_STATE, action) => {
1617
return { ...state, data: [...state.data, action.note] }
1718
}
1819

20+
const remove = (state = INITIAL_STATE, action) => {
21+
return { ...state, data: state.data.filter(note => note.id !== action.id) }
22+
}
23+
1924
/**
2025
* Criando os reducers
2126
* Ex.: [Types.ADD]: add == switch actions.type
@@ -27,4 +32,5 @@ const add = (state = INITIAL_STATE, action) => {
2732

2833
export default createReducer(INITIAL_STATE, {
2934
[Types.ADD]: add,
35+
[Types.REMOVE]: remove
3036
})

0 commit comments

Comments
 (0)