我在管理添加操作时遇到问题
如果要加载,请使用useReducer和useEffect
我已经解决的动作
const [todos, dispatch] = useReducer(reducer, initialTodoState);
useEffect(() => {
const fetchTodos = async () => {
const response = await fetch(url);
const todos = await response.json();
dispatch({ type: Action.LOAD, payload: todos });
};
fetchTodos();
}, []);
但是通过todo值使用useEffect的方式是什么? 这是正确的方法吗?
const [todo, setTodo] = useState("");
useEffect(() => {
if(todo){
// fetch data
}
}, [todo]);
const submitHandler = (todo: string) => {
setTodo(todo);
};