事件处理函数
一句话解释
事件处理函数是 函数那个运行 in 响应到 用户操作 (点击, 类型, 提交) — attached 到元素通过 Props 如 onClick.
用大白话说
In React, 你 handle 用户事件通过 passing 函数 as prop named 之后事件: onClick, onChange, onSubmit, onMouseOver, etc. When the event fires, React calls your function with a synthetic event object.
React's synthetic events wrap the browser's native events to provide a consistent API across browsers. They have the same interface (e.目标, e.preventDefault(), e.stopPropagation()) but are pooled for performance — in older React versions you had to call e.persist() to use them asynchronously.
A common pattern is to define the handler inline or as a method and pass it to the element. For performance, you may wrap handlers in useCallback to avoid creating new function instances on every render.
示例
function Form() {
const [text, setText] = useState("");
return (
<input
value={text}
onChange={(e) => setText(e.target.value)}
/>
);
}onChange fires on 每个 keystroke. 处理函数读取 e.目标.值 (输入's current 文本) and 更新状态.
相关术语
相关资源
相关课程
- 事件 Handling
学习 React 事件处理函数 and synthetic 事件.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。