N
N
Nikita2019-09-18 10:55:39
React
Nikita, 2019-09-18 10:55:39

Editing not working, how to solve?

I want to add an editing function to the todo app, but when I change a note, undefined comes up.
Here is the action creator:

export const changeTodo = (id, text) => {
  debugger
  return {
    type: CHANGE_TODO,
    id,
    text
  }
}

Here is the Reducer:
case CHANGE_TODO: 
    debugger
      return {
        ...state,
        todos: state.todos.map(todo => todo.id === action.id
          ? { ...todo, id: todo.id, text: action.text, completed: todo.completed }
          : todo
        )
      }

The problem is that I don't pass the parameter (which will contain the value) to the Todo component, and if I use args, then the interpreter swears.
Please tell me how to solve this problem.
All applications on github: GitHub
const TodoList = ({removeTodo, toggleTodo, changeTodo, todos}) => {
  return (
    <StyledTodoList>
      <Ul>
        {
          todos.map(todo => 
            <Todo
            key={todo.id}
            toggleHandler={() => toggleTodo(todo.id)}
            removeHandler={() => removeTodo(todo.id)}
            сhangeHandler={() => changeTodo(todo.id)}
            // для для изменения заметки нужно добавит еще один параметр, который будет новым значением
            {...todo}
            />
          )
        }
      </Ul>
      <AddButton>
        <NavLink 
        to='addTodo'
        >+</NavLink>
      </AddButton>
    </StyledTodoList>
  )
}

And Todo:
const Todo = props => {

  const { id, text, toggleHandler, removeHandler, сhangeHandler} = props
  const [isEditingStarted, setEditValue] = useState(false)
  const [inputValue, setInputValue] = useState('')

  const submitHandler = event => {
    event.preventDefault()
    сhangeHandler(id, inputValue)
    setInputValue('')
  }

  return (
    <StyledTodo {...props}>
      <StyledCheckBox
        type='checkbox'
        onClick={toggleHandler}
      />
      {isEditingStarted ? 
      <form onSubmit={submitHandler}>
        <Input 
        defaultValue={text}
        onChange={e => setInputValue(e.target.value)}
        onKeyDown = {e => e.keyCode === 27 ? setEditValue(false) : ''}
        /> 
      </form> :
      text
      }
      <div>
        <Span 
         onClick={() => setEditValue(true)}
         >
          <img 
          src={changeIcon} 
          alt='Ред'
          />
        </Span>
        <Span
          onClick={removeHandler}
        >
          <img 
          src={closeIcon} 
          alt='Закрыть'
          />
        </Span>
      </div>
    </StyledTodo>
  )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-09-18
@SonyFan23

Replace
with
сhangeHandler={changeTodo}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question