Answer the question
In order to leave comments, you need to log in
How to port a function to Redux?
Practicing with Redux, making a simple todo-list. I started to integrate the whole thing into Redux, and realized that I had not yet fully understood its logic. Generally. There are three main actions:
const mapDispatchToProps = (dispatch) => {
return {
addTodo: (todo) => dispatch({ type: 'ADD_TODO', todo: todo }),
handleChange: (e) => dispatch({ type: 'HANDLE_CHANGE', e: e }),
handleSubmit: (e) => dispatch({ type: 'HANDLE_SUBMIT', e: e })
}
}
const reducerTodoList = (state = initialState, action) => {
const newState = {...state};
switch (action.type) {
case 'DEL_TODO':
const todos = state.todos.filter(todo => {
return todo.id !== action.id;
});
return {
todos: todos
}
case 'ADD_TODO':
action.todo = Math.random();
const todoos = [...state.todos, action.todo]
return {
todos: todoos
}
case 'HANDLE_CHANGLE':
return {
content: action.e.target.value
}
case 'HANDLE_SUBMIT':
action.e.preventDefault();
return {
...
}
default:
return state;
}
}
handleSubmit = (e) => {
e.preventDefault();
this.props.addTodo(this.state);
this.setState({
content: ''
})
Answer the question
In order to leave comments, you need to log in
handleChange and handleSubmit have nothing to do in the reducer. Leave these handlers in the component.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question