【发布时间】:2020-12-05 02:23:29
【问题描述】:
在video.js的官方文档https://docs.videojs.com/tutorial-react.html
我们有
componentDidMount() {
// instantiate Video.js
this.player = videojs(this.videoNode, this.props, function onPlayerReady() {
console.log('onPlayerReady', this)
});
}
我想用钩子创建功能组件
export default function VideoPlayer(props) {
const player = useRef(null);
const videoNode = useRef(null);
useEffect(() => {
player.current = videojs(videoNode.current, props);
return () => {
if (player.current) {
player.current.dispose()
}
}
}, []);//I have problem with dependency array
return (
<div data-vjs-player>
<video ref={videoNode} className="video-js"/>
</div>
)
}
我有警告
ESLint:React Hook useEffect 缺少依赖项:'props'。要么包含它,要么移除依赖数组。(react-hooks/exhaustive-deps)
如果我将依赖数组从 [] 更改为 [props] useEffect 在每次渲染时运行,我只想在第一次运行它,例如 componentDidMount
如何使用钩子准确地创建componentDidMount?
【问题讨论】:
标签: reactjs react-hooks