I've been trying to implement redux and I can post data into the database using redux, and then fetching the results in react. But I'm having difficulty on how to approach the reducer and implementing a delete case.
I had previously attempted to use a function that would delete data in the database through sqlite, but with mixed results. If my memory serves me correct, I could possibly use a prototype method to remove the data from the database, without mutating the state.
import request from 'superagent'
export function receiveData(data) {
return {
type: 'RECEIVE_DATA',
payload: data
}
}
export function fetchData() {
return (dispatch) => {
request.get('/api/v1/data')
.then((res) => {
return res.body
}).then(data => {
dispatch(receiveCats(data))
})
}
}
function reducer(state = [], action) {
switch (action.type) {
case 'RECEIVE_DATA':
return action.payload
case 'ADD_DATA':
return action.add
default:
return state
}
}
// case 'DELETE_CAT':
////// return action.del
// to be added
export default reducer
export function addData(data) {
return dispatch => {
request
.post('/api/v1/data')
.send(data)
.end((err, res) => {
if (err) {
console.error(err.message)
return
}
dispatch(addDataAction(data))
})
}
}
function addDataAction() {
return {
type: 'ADD_DATA',
payload: {
name: name,
// age: age,
// address: address
}
}
}
I want to understand how redux works but this a hurdle for me.
Any help would be much appreciated. Thanks.