React automatically updates the DOM to match your render output, so your components won’t often need to manipulate it. However, sometimes you might need access to the DOM elements managed by React—for example, to focus a node, scroll to it, or measure its size and position. There is no built-in way to do those things in React, so you will need a ref to the DOM node. 由于 React 会自动处理更新 DOM 以匹配你的渲染输出,因此你在组件中通常不需要操作 DOM。但是,有时你可能需要访问由 React 管理的 DOM 元素 —— 例如,让一个节点获得焦点、滚动到它或测量它的尺寸和位置。在 React 中没有内置的方法来做这些事情,所以你需要一个指向 DOM 节点的 ref 来实现。
你将会学习到
- How to access a DOM node managed by React with the
ref
attribute - 如何使用
ref
属性访问由 React 管理的 DOM 节点 - How the
ref
JSX attribute relates to theuseRef
Hook ref
JSX 属性如何与useRef
Hook 相关联- How to access another component’s DOM node
- 如何访问另一个组件的 DOM 节点
- In which cases it’s safe to modify the DOM managed by React
- 在哪些情况下修改 React 管理的 DOM 是安全的
获取指向节点的 ref | Getting a ref to the node
To access a DOM node managed by React, first, import the useRef
Hook:
要访问由 React 管理的 DOM 节点,首先,引入 useRef
Hook:
import { useRef } from 'react';
Then, use it to declare a ref inside your component: 然后,在你的组件中使用它声明一个 ref:
const myRef = useRef(null);
Finally, pass your ref as the ref
attribute to the JSX tag for which you want to get the DOM node:
最后,将 ref 作为 ref
属性值传递给想要获取的 DOM 节点的 JSX 标签:
<div ref={myRef}>
The useRef
Hook returns an object with a single property called current
. Initially, myRef.current
will be null
. When React creates a DOM node for this <div>
, React will put a reference to this node into myRef.current
. You can then access this DOM node from your event handlers and use the built-in browser APIs defined on it.
useRef
Hook 返回一个对象,该对象有一个名为 current
的属性。最初,myRef.current
是 null
。当 React 为这个 <div>
创建一个 DOM 节点时,React 会把对该节点的引用放入 myRef.current
。然后,你可以从 事件处理器 访问此 DOM 节点,并使用在其上定义的内置浏览器 API。
// You can use any browser APIs, for example:
// 你可以使用任意浏览器 API,例如:
myRef.current.scrollIntoView();
示例: 使文本输入框获得焦点 | Example: Focusing a text input
In this example, clicking the button will focus the input: 在本例中,单击按钮将使输入框获得焦点:
import { useRef } from 'react'; export default function Form() { const inputRef = useRef(null); function handleClick() { inputRef.current.focus(); } return ( <> <input ref={inputRef} /> <button onClick={handleClick}> 聚焦输入框 </button> </> ); }
To implement this: 要实现这一点:
- Declare
inputRef
with theuseRef
Hook.
- 使用
useRef
Hook 声明inputRef
。
- Pass it as
<input ref={inputRef}>
. This tells React to put this<input>
’s DOM node intoinputRef.current
.
- 像
<input ref={inputRef}>
这样传递它。这告诉 React 将这个<input>
的 DOM 节点放入inputRef.current
。
- In the
handleClick
function, read the input DOM node frominputRef.current
and callfocus()
on it withinputRef.current.focus()
.
- 在
handleClick
函数中,从inputRef.current
读取 input DOM 节点并使用inputRef.current.focus()
调用它的focus()
。
- Pass the
handleClick
event handler to<button>
withonClick
.
- 用
onClick
将handleClick
事件处理器传递给<button>
。
While DOM manipulation is the most common use case for refs, the useRef
Hook can be used for storing other things outside React, like timer IDs. Similarly to state, refs remain between renders. Refs are like state variables that don’t trigger re-renders when you set them. Read about refs in Referencing Values with Refs.
虽然 DOM 操作是 ref 最常见的用例,但 useRef
Hook 可用于存储 React 之外的其他内容,例如计时器 ID 。与 state 类似,ref 能在渲染之间保留。你甚至可以将 ref 视为设置它们时不会触发重新渲染的 state 变量!你可以在使用 Ref 引用值中了解有关 ref 的更多信息。
示例: 滚动至一个元素 | Example: Scrolling to an element
You can have more than a single ref in a component. In this example, there is a carousel of three images. Each button centers an image by calling the browser scrollIntoView()
method on the corresponding DOM node:
一个组件中可以有多个 ref。在这个例子中,有一个由三张图片和三个按钮组成的轮播,点击按钮会调用浏览器的 scrollIntoView()
方法,在相应的 DOM 节点上将它们居中显示在视口中:
import { useRef } from 'react'; export default function CatFriends() { const firstCatRef = useRef(null); const secondCatRef = useRef(null); const thirdCatRef = useRef(null); function handleScrollToFirstCat() { firstCatRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' }); } function handleScrollToSecondCat() { secondCatRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' }); } function handleScrollToThirdCat() { thirdCatRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' }); } return ( <> <nav> <button onClick={handleScrollToFirstCat}> Neo </button> <button onClick={handleScrollToSecondCat}> Millie </button> <button onClick={handleScrollToThirdCat}> Bella </button> </nav> <div> <ul> <li> <img src="https://placecats.com/neo/300/200" alt="Neo" ref={firstCatRef} /> </li> <li> <img src="https://placecats.com/millie/200/200" alt="Millie" ref={secondCatRef} /> </li> <li> <img src="https://placecats.com/bella/199/200" alt="Bella" ref={thirdCatRef} /> </li> </ul> </div> </> ); }
深入探讨
In the above examples, there is a predefined number of refs. However, sometimes you might need a ref to each item in the list, and you don’t know how many you will have. Something like this wouldn’t work: 在上面的例子中,ref 的数量是预先确定的。但有时候,你可能需要为列表中的每一项都绑定 ref ,而你又不知道会有多少项。像下面这样做是行不通的:
<ul>
{items.map((item) => {
// Doesn't work!
// 行不通!
const ref = useRef(null);
return <li ref={ref} />;
})}
</ul>
This is because Hooks must only be called at the top-level of your component. You can’t call useRef
in a loop, in a condition, or inside a map()
call.
这是因为 Hook 只能在组件的顶层被调用。不能在循环语句、条件语句或 map()
函数中调用 useRef
。
One possible way around this is to get a single ref to their parent element, and then use DOM manipulation methods like querySelectorAll
to “find” the individual child nodes from it. However, this is brittle and can break if your DOM structure changes.
一种可能的解决方案是用一个 ref 引用其父元素,然后用 DOM 操作方法如 querySelectorAll
来寻找它的子节点。然而,这种方法很脆弱,如果 DOM 结构发生变化,可能会失效或报错。
Another solution is to pass a function to the ref
attribute. This is called a ref
callback. React will call your ref callback with the DOM node when it’s time to set the ref, and with null
when it’s time to clear it. This lets you maintain your own array or a Map, and access any ref by its index or some kind of ID.
另一种解决方案是将函数传递给 ref
属性。这称为 ref
回调。当需要设置 ref 时,React 将传入 DOM 节点来调用你的 ref 回调,并在需要清除它时传入 null
。这使你可以维护自己的数组或 Map,并通过其索引或某种类型的 ID 访问任何 ref。
This example shows how you can use this approach to scroll to an arbitrary node in a long list: 此示例展示了如何使用此方法滚动到长列表中的任意节点:
import { useRef, useState } from "react"; export default function CatFriends() { const itemsRef = useRef(null); const [catList, setCatList] = useState(setupCatList); function scrollToCat(cat) { const map = getMap(); const node = map.get(cat); node.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "center", }); } function getMap() { if (!itemsRef.current) { // Initialize the Map on first usage. // 首次运行时初始化 Map。 itemsRef.current = new Map(); } return itemsRef.current; } return ( <> <nav> <button onClick={() => scrollToCat(catList[0])}>Neo</button> <button onClick={() => scrollToCat(catList[5])}>Millie</button> <button onClick={() => scrollToCat(catList[9])}>Bella</button> </nav> <div> <ul> {catList.map((cat) => ( <li key={cat} ref={(node) => { const map = getMap(); if (node) { map.set(cat, node); } else { map.delete(cat); } }} > <img src={cat} /> </li> ))} </ul> </div> </> ); } function setupCatList() { const catList = []; for (let i = 0; i < 10; i++) { catList.push("https://loremflickr.com/320/240/cat?lock=" + i); } return catList; }
In this example, itemsRef
doesn’t hold a single DOM node. Instead, it holds a Map from item ID to a DOM node. (Refs can hold any values!) The ref
callback on every list item takes care to update the Map:
在这个例子中,itemsRef
保存的不是单个 DOM 节点,而是保存了包含列表项 ID 和 DOM 节点的 Map。(Ref 可以保存任何值!) 每个列表项上的 ref
回调负责更新 Map:
<li
key={cat.id}
ref={node => {
const map = getMap();
if (node) {
// Add to the Map
map.set(cat, node);
} else {
// Remove from the Map
map.delete(cat);
}
}}
>
This lets you read individual DOM nodes from the Map later. 这使你可以之后从 Map 读取单个 DOM 节点。
访问另一个组件的 DOM 节点 | Accessing another component’s DOM nodes
When you put a ref on a built-in component that outputs a browser element like <input />
, React will set that ref’s current
property to the corresponding DOM node (such as the actual <input />
in the browser).
当你将 ref 放在像 <input />
这样输出浏览器元素的内置组件上时,React 会将该 ref 的 current
属性设置为相应的 DOM 节点(例如浏览器中实际的 <input />
)。
However, if you try to put a ref on your own component, like <MyInput />
, by default you will get null
. Here is an example demonstrating it. Notice how clicking the button does not focus the input:
但是,如果你尝试将 ref 放在 你自己的 组件上,例如 <MyInput />
,默认情况下你会得到 null
。这个示例演示了这种情况。请注意单击按钮 并不会 聚焦输入框:
import { useRef } from 'react'; function MyInput(props) { return <input {...props} />; } export default function MyForm() { const inputRef = useRef(null); function handleClick() { inputRef.current.focus(); } return ( <> <MyInput ref={inputRef} /> <button onClick={handleClick}> 聚焦输入框 </button> </> ); }
To help you notice the issue, React also prints an error to the console: 为了帮助你注意到这个问题,React 还会向控制台打印一条错误消息:
This happens because by default React does not let a component access the DOM nodes of other components. Not even for its own children! This is intentional. Refs are an escape hatch that should be used sparingly. Manually manipulating another component’s DOM nodes makes your code even more fragile. 发生这种情况是因为默认情况下,React 不允许组件访问其他组件的 DOM 节点。甚至自己的子组件也不行!这是故意的。Refs 是一种脱围机制,应该谨慎使用。手动操作 另一个 组件的 DOM 节点会使你的代码更加脆弱。
Instead, components that want to expose their DOM nodes have to opt in to that behavior. A component can specify that it “forwards” its ref to one of its children. Here’s how MyInput
can use the forwardRef
API:
相反,想要 暴露其 DOM 节点的组件必须选择该行为。一个组件可以指定将它的 ref “转发”给一个子组件。下面是 MyInput
如何使用 forwardRef
API:
const MyInput = forwardRef((props, ref) => {
return <input {...props} ref={ref} />;
});
This is how it works: 它是这样工作的:
<MyInput ref={inputRef} />
tells React to put the corresponding DOM node intoinputRef.current
. However, it’s up to theMyInput
component to opt into that—by default, it doesn’t.
<MyInput ref={inputRef} />
告诉 React 将对应的 DOM 节点放入inputRef.current
中。但是,这取决于MyInput
组件是否允许这种行为, 默认情况下是不允许的。
- The
MyInput
component is declared usingforwardRef
. This opts it into receiving theinputRef
from above as the secondref
argument which is declared afterprops
.
MyInput
组件是使用forwardRef
声明的。 这让从上面接收的inputRef
作为第二个参数ref
传入组件,第一个参数是props
。
MyInput
itself passes theref
it received to the<input>
inside of it.
MyInput
组件将自己接收到的ref
传递给它内部的<input>
。
Now clicking the button to focus the input works: 现在,单击按钮聚焦输入框起作用了:
import { forwardRef, useRef } from 'react'; const MyInput = forwardRef((props, ref) => { return <input {...props} ref={ref} />; }); export default function Form() { const inputRef = useRef(null); function handleClick() { inputRef.current.focus(); } return ( <> <MyInput ref={inputRef} /> <button onClick={handleClick}> 聚焦输入框 </button> </> ); }
In design systems, it is a common pattern for low-level components like buttons, inputs, and so on, to forward their refs to their DOM nodes. On the other hand, high-level components like forms, lists, or page sections usually won’t expose their DOM nodes to avoid accidental dependencies on the DOM structure. 在设计系统中,将低级组件(如按钮、输入框等)的 ref 转发到它们的 DOM 节点是一种常见模式。另一方面,像表单、列表或页面段落这样的高级组件通常不会暴露它们的 DOM 节点,以避免对 DOM 结构的意外依赖。
深入探讨
In the above example, MyInput
exposes the original DOM input element. This lets the parent component call focus()
on it. However, this also lets the parent component do something else—for example, change its CSS styles. In uncommon cases, you may want to restrict the exposed functionality. You can do that with useImperativeHandle
:
在上面的例子中,MyInput
暴露了原始的 DOM 元素 input。这让父组件可以对其调用focus()
。然而,这也让父组件能够做其他事情 —— 例如,改变其 CSS 样式。在一些不常见的情况下,你可能希望限制暴露的功能。你可以用 useImperativeHandle
做到这一点:
import { forwardRef, useRef, useImperativeHandle } from 'react'; const MyInput = forwardRef((props, ref) => { const realInputRef = useRef(null); useImperativeHandle(ref, () => ({ // Only expose focus and nothing else // 只暴露 focus,没有别的 focus() { realInputRef.current.focus(); }, })); return <input {...props} ref={realInputRef} />; }); export default function Form() { const inputRef = useRef(null); function handleClick() { inputRef.current.focus(); } return ( <> <MyInput ref={inputRef} /> <button onClick={handleClick}> 聚焦输入框 </button> </> ); }
Here, realInputRef
inside MyInput
holds the actual input DOM node. However, useImperativeHandle
instructs React to provide your own special object as the value of a ref to the parent component. So inputRef.current
inside the Form
component will only have the focus
method. In this case, the ref “handle” is not the DOM node, but the custom object you create inside useImperativeHandle
call.
这里,MyInput
中的 realInputRef
保存了实际的 input DOM 节点。 但是,useImperativeHandle
指示 React 将你自己指定的对象作为父组件的 ref 值。 所以 Form
组件内的 inputRef.current
将只有 focus
方法。在这种情况下,ref “句柄”不是 DOM 节点,而是你在 useImperativeHandle
调用中创建的自定义对象。
React 何时添加 refs | When React attaches the refs
In React, every update is split in two phases: 在 React 中,每次更新都分为 两个阶段:
- During render, React calls your components to figure out what should be on the screen.
- 在 渲染 阶段, React 调用你的组件来确定屏幕上应该显示什么。
- During commit, React applies changes to the DOM.
- 在 提交 阶段, React 把变更应用于 DOM。
In general, you don’t want to access refs during rendering. That goes for refs holding DOM nodes as well. During the first render, the DOM nodes have not yet been created, so ref.current
will be null
. And during the rendering of updates, the DOM nodes haven’t been updated yet. So it’s too early to read them.
通常,你 不希望 在渲染期间访问 refs。这也适用于保存 DOM 节点的 refs。在第一次渲染期间,DOM 节点尚未创建,因此 ref.current
将为 null
。在渲染更新的过程中,DOM 节点还没有更新。所以读取它们还为时过早。
React sets ref.current
during the commit. Before updating the DOM, React sets the affected ref.current
values to null
. After updating the DOM, React immediately sets them to the corresponding DOM nodes.
React 在提交阶段设置 ref.current
。在更新 DOM 之前,React 将受影响的 ref.current
值设置为 null
。更新 DOM 后,React 立即将它们设置到相应的 DOM 节点。
Usually, you will access refs from event handlers. If you want to do something with a ref, but there is no particular event to do it in, you might need an Effect. We will discuss Effects on the next pages. 通常,你将从事件处理器访问 refs。 如果你想使用 ref 执行某些操作,但没有特定的事件可以执行此操作,你可能需要一个 effect。我们将在下一页讨论 effect。
深入探讨
Consider code like this, which adds a new todo and scrolls the screen down to the last child of the list. Notice how, for some reason, it always scrolls to the todo that was just before the last added one: 思考这样的代码,它添加一个新的待办事项,并将屏幕向下滚动到列表的最后一个子项。请注意,出于某种原因,它总是滚动到最后一个添加 之前 的待办事项:
import { useState, useRef } from 'react'; export default function TodoList() { const listRef = useRef(null); const [text, setText] = useState(''); const [todos, setTodos] = useState( initialTodos ); function handleAdd() { const newTodo = { id: nextId++, text: text }; setText(''); setTodos([ ...todos, newTodo]); listRef.current.lastChild.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); } return ( <> <button onClick={handleAdd}> 添加 </button> <input value={text} onChange={e => setText(e.target.value)} /> <ul ref={listRef}> {todos.map(todo => ( <li key={todo.id}>{todo.text}</li> ))} </ul> </> ); } let nextId = 0; let initialTodos = []; for (let i = 0; i < 20; i++) { initialTodos.push({ id: nextId++, text: '待办 #' + (i + 1) }); }
The issue is with these two lines: 问题出在这两行:
setTodos([ ...todos, newTodo]);
listRef.current.lastChild.scrollIntoView();
In React, state updates are queued. Usually, this is what you want. However, here it causes a problem because setTodos
does not immediately update the DOM. So the time you scroll the list to its last element, the todo has not yet been added. This is why scrolling always “lags behind” by one item.
在 React 中,state 更新是排队进行的。通常,这就是你想要的。但是,在这个示例中会导致问题,因为 setTodos
不会立即更新 DOM。因此,当你将列表滚动到最后一个元素时,尚未添加待办事项。这就是滚动总是“落后”一项的原因。
To fix this issue, you can force React to update (“flush”) the DOM synchronously. To do this, import flushSync
from react-dom
and wrap the state update into a flushSync
call:
要解决此问题,你可以强制 React 同步更新(“刷新”)DOM。 为此,从 react-dom
导入 flushSync
并将 state 更新包裹 到 flushSync
调用中:
flushSync(() => {
setTodos([ ...todos, newTodo]);
});
listRef.current.lastChild.scrollIntoView();
This will instruct React to update the DOM synchronously right after the code wrapped in flushSync
executes. As a result, the last todo will already be in the DOM by the time you try to scroll to it:
这将指示 React 当封装在 flushSync
中的代码执行后,立即同步更新 DOM。因此,当你尝试滚动到最后一个待办事项时,它已经在 DOM 中了:
import { useState, useRef } from 'react'; import { flushSync } from 'react-dom'; export default function TodoList() { const listRef = useRef(null); const [text, setText] = useState(''); const [todos, setTodos] = useState( initialTodos ); function handleAdd() { const newTodo = { id: nextId++, text: text }; flushSync(() => { setText(''); setTodos([ ...todos, newTodo]); }); listRef.current.lastChild.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); } return ( <> <button onClick={handleAdd}> 添加 </button> <input value={text} onChange={e => setText(e.target.value)} /> <ul ref={listRef}> {todos.map(todo => ( <li key={todo.id}>{todo.text}</li> ))} </ul> </> ); } let nextId = 0; let initialTodos = []; for (let i = 0; i < 20; i++) { initialTodos.push({ id: nextId++, text: '待办 #' + (i + 1) }); }
使用 refs 操作 DOM 的最佳实践 | Best practices for DOM manipulation with refs
Refs are an escape hatch. You should only use them when you have to “step outside React”. Common examples of this include managing focus, scroll position, or calling browser APIs that React does not expose. Refs 是一种脱围机制。你应该只在你必须“跳出 React”时使用它们。这方面的常见示例包括管理焦点、滚动位置或调用 React 未暴露的浏览器 API。
If you stick to non-destructive actions like focusing and scrolling, you shouldn’t encounter any problems. However, if you try to modify the DOM manually, you can risk conflicting with the changes React is making. 如果你坚持聚焦和滚动等非破坏性操作,应该不会遇到任何问题。但是,如果你尝试手动修改 DOM,则可能会与 React 所做的更改发生冲突。
To illustrate this problem, this example includes a welcome message and two buttons. The first button toggles its presence using conditional rendering and state, as you would usually do in React. The second button uses the remove()
DOM API to forcefully remove it from the DOM outside of React’s control.
为了说明这个问题,这个例子包括一条欢迎消息和两个按钮。第一个按钮使用 条件渲染 和 state 切换它的显示和隐藏,就像你通常在 React 中所做的那样。第二个按钮使用 remove()
DOM API 将其从 React 控制之外的 DOM 中强行移除.
Try pressing “Toggle with setState” a few times. The message should disappear and appear again. Then press “Remove from the DOM”. This will forcefully remove it. Finally, press “Toggle with setState”: 尝试按几次“通过 setState 切换”。该消息会消失并再次出现。然后按 “从 DOM 中删除”。这将强行删除它。最后,按 “通过 setState 切换”:
import { useState, useRef } from 'react'; export default function Counter() { const [show, setShow] = useState(true); const ref = useRef(null); return ( <div> <button onClick={() => { setShow(!show); }}> 通过 setState 切换 </button> <button onClick={() => { ref.current.remove(); }}> 从 DOM 中删除 </button> {show && <p ref={ref}>Hello world</p>} </div> ); }
After you’ve manually removed the DOM element, trying to use setState
to show it again will lead to a crash. This is because you’ve changed the DOM, and React doesn’t know how to continue managing it correctly.
在你手动删除 DOM 元素后,尝试使用 setState
再次显示它会导致崩溃。这是因为你更改了 DOM,而 React 不知道如何继续正确管理它。
Avoid changing DOM nodes managed by React. Modifying, adding children to, or removing children from elements that are managed by React can lead to inconsistent visual results or crashes like above. 避免更改由 React 管理的 DOM 节点。 对 React 管理的元素进行修改、添加子元素、从中删除子元素会导致不一致的视觉结果,或与上述类似的崩溃。
However, this doesn’t mean that you can’t do it at all. It requires caution. You can safely modify parts of the DOM that React has no reason to update. For example, if some <div>
is always empty in the JSX, React won’t have a reason to touch its children list. Therefore, it is safe to manually add or remove elements there.
但是,这并不意味着你完全不能这样做。它需要谨慎。 你可以安全地修改 React 没有理由更新的部分 DOM。 例如,如果某些 <div>
在 JSX 中始终为空,React 将没有理由去变动其子列表。 因此,在那里手动增删元素是安全的。
摘要
- Refs are a generic concept, but most often you’ll use them to hold DOM elements.
- Refs 是一个通用概念,但大多数情况下你会使用它们来保存 DOM 元素。
- You instruct React to put a DOM node into
myRef.current
by passing<div ref={myRef}>
. - 你通过传递
<div ref={myRef}>
指示 React 将 DOM 节点放入myRef.current
。 - Usually, you will use refs for non-destructive actions like focusing, scrolling, or measuring DOM elements.
- 通常,你会将 refs 用于非破坏性操作,例如聚焦、滚动或测量 DOM 元素。
- A component doesn’t expose its DOM nodes by default. You can opt into exposing a DOM node by using
forwardRef
and passing the secondref
argument down to a specific node. - 默认情况下,组件不暴露其 DOM 节点。 你可以通过使用
forwardRef
并将第二个ref
参数传递给特定节点来暴露 DOM 节点。 - Avoid changing DOM nodes managed by React.
- 避免更改由 React 管理的 DOM 节点。
- If you do modify DOM nodes managed by React, modify parts that React has no reason to update.
- 如果你确实修改了 React 管理的 DOM 节点,请修改 React 没有理由更新的部分。
第 1 个挑战 共 4 个挑战: 播放和暂停视频 | Play and pause the video
In this example, the button toggles a state variable to switch between a playing and a paused state. However, in order to actually play or pause the video, toggling state is not enough. You also need to call play()
and pause()
on the DOM element for the <video>
. Add a ref to it, and make the button work.
在此示例中,按钮切换 state 变量以在播放和暂停状态之间切换。 然而,为了实际播放或暂停视频,切换状态是不够的。你还需要在 <video>
的 DOM 元素上调用 play()
和 pause()
。 向它添加一个 ref,并使按钮起作用。
import { useState, useRef } from 'react'; export default function VideoPlayer() { const [isPlaying, setIsPlaying] = useState(false); function handleClick() { const nextIsPlaying = !isPlaying; setIsPlaying(nextIsPlaying); } return ( <> <button onClick={handleClick}> {isPlaying ? '暂停' : '播放'} </button> <video width="250"> <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4" /> </video> </> ) }
For an extra challenge, keep the “Play” button in sync with whether the video is playing even if the user right-clicks the video and plays it using the built-in browser media controls. You might want to listen to onPlay
and onPause
on the video to do that.
对于额外的挑战,即使用户右键单击视频并使用内置浏览器媒体控件播放,也要使“播放”按钮与视频是否正在播放同步。 你可能需要在视频中监听 onPlay
和 onPause
才能做到这一点。