Answer the question
In order to leave comments, you need to log in
Why is the context lost in the handler?
There is an input field, I hang the 'keyup' event on it, after which the text of this input is added to the store. The problem is that in the addTodo function, the text value becomes empty (this can be seen from the output to the console). As a result, an empty string is left in store.
function Header(props) {
const [title, setTitle] = useState('');
const titleRef = React.createRef();
const keyCodeEnter = 13;
useEffect(() => {
titleRef.current.addEventListener('keyup', addTodo);
return () => titleRef.current.removeEventListener('keyup', addTodo);
}, [])
function addTodo(e) {
console.log(title) // ТУТ ПУСТО
if(e.keyCode === keyCodeEnter) {
props.addTodoCreator({
title: title,
isCompleted: false,
color: 0,
});
}
}
return (
<>
<input
type="text"
value={ title }
onChange={ e => setTitle(e.target.value) }
ref={ titleRef }
/>
</>
);
}
Answer the question
In order to leave comments, you need to log in
1. Why are you attaching a listener to a ref?
useEffect(() => {
titleRef.current.addEventListener('keyup', addTodo); // <-- ???
return () => titleRef.current.removeEventListener('keyup', addTodo);
}, [])
return (
<> // <-- ???
<input
type="text"
value={ title }
onChange={ e => setTitle(e.target.value) }
ref={ titleRef }
/>
</> // <-- ???
);
function addTodo(e) {
if(e.keyCode === keyCodeEnter) { // <-- e.key === "Enter"
props.addTodoCreator({
title: title,
isCompleted: false,
color: 0,
});
}
}
const Header = ({ addTodoCreator }) => {
const [title, setTitle] = useState("");
const addTodo = (e) => {
if (e.key === "Enter") {
addTodoCreator({
title: title,
isCompleted: false,
color: 0,
});
}
};
return (
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
onKeyUp={addTodo}
/>
);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question