2 回答
TA贡献1770条经验 获得超3个赞
提交待办事项后清空输入即可。
const AddTodoForm = () => {
const inputRef = useRef()
const createTodo = (e) => {
e.preventDefault()
const todoRef = fire.database().ref("Todo");
const todo = {
title: inputRef.current.value,
complete: false
};
todoRef.push(todo)
inputRef.current.value = ""
}
return (
<form>
<input type="text" ref={inputRef} />
<button onClick={createTodo}> Add Todo </button>
</form>
)
}
TA贡献1828条经验 获得超4个赞
只需在 rer 中获取当前 DOM 并设置值“”( inputRef.current.value = "";)。例子:
import React, { useRef, useState } from "react";
export default function DisableElevation() {
const [todos, setTodos] = useState([]);
const addTodo = (todo) => {
setTodos([...todos, todo]);
};
return (
<div>
<AddTodoForm addTodo={(todo) => addTodo(todo)} />
{todos.map((todo) => (
<div> {todo.title} </div>
))}
</div>
);
}
const AddTodoForm = ({ addTodo }) => {
const inputRef = useRef();
const createTodo = (e) => {
e.preventDefault();
const todo = {
title: inputRef.current.value,
complete: false
};
inputRef.current.value = "";
addTodo(todo);
// I Need To Empty input value here
};
return (
<form>
<input type="text" ref={inputRef} />
<button onClick={createTodo}> Add Todo </button>
</form>
);
};
添加回答
举报