I'm making a todo app with react-redux now. For now, I have 4 inputs and want to combine each input value into one and manage it with react-redux. But I keep failing. In this case, What should I do?
The data structure I want to make is like this.
const initialState = {
list: [
{
id: 0,
dateData: "",
timeData: "",
title: "",
desc: "",
},
]
};
reducer :
export const setDate = value => ({
type: SET_DATE_DATA,
payload: {
value
},
})
export const setTime = value => ({
type: SET_TIME_DATA,
payload: {
value
},
})
export const setTitle = value => ({
type: SET_TITLE,
payload: {
value
},
})
export const setDesc = value => ({
type: SET_DESC,
payload: {
value
},
})
const initialState = {
list: [],
}
export default function todoReducer (state=initialState, action) {
switch (action.type) {
case SET_DATE_DATA:
return {
...state,
list: state.list.concat({
dateData: action.payload.value,
}),
};
case SET_TIME_DATA:
return {
...state,
list: state.list.concat({
timeData: action.payload.value,
}),
};
case SET_TITLE:
return {
...state,
list: state.list.concat({
title: action.payload.value,
}),
};
case SET_DESC:
return {
...state,
list: state.list.concat({
desc: action.payload.value,
}),
};
}
}
TodoContainer : This logic is passed to the todo component and used as the onChange prop of the input.
const setInput = (e) => {
const { name, value } = e.target
switch (name) {
case "todo-date":
return alarmActions.setDate(value);
case "todo-time":
return alarmActions.setTime(value);
case "todo-title":
return alarmActions.setTitle(value);
case "todo-desc":
return alarmActions.setDesc(value);
}
}
React.useStateand upon submitting the form set the whole data to your redux.