有没有办法在React组件函数中实现全屏API?
答案 0 :(得分:0)
我可以向您推荐这个软件包react-full-screen
答案 1 :(得分:0)
您可以参考 @chiragrupani/fullscreen-react 包。它支持类组件和钩子/函数组件。此外,它支持允许多个元素进入全屏。它的用法很简单:
安装:npm i @chiragrupani/fullscreen-react
<button onClick={e => this.setState({isFullScreen: true})}>Go Fullscreen</button>
<FullScreen
isFullScreen={this.state.isFullScreen}
onChange={(isFullScreen) => {
this.setState({ isFullScreen });
}}
>
<div>Fullscreen</div>
</FullScreen>
对于钩子,代码类似:
export default function FSExampleHook() {
let [isFullScreen, setFullScreen] = React.useState(false);
return (
<>
<button onClick={(e) => setFullScreen(true)}>Go Fullscreen</button>
<FullScreen
isFullScreen={isFullScreen}
onChange={(isFull: boolean) => {
setFullScreen(isFull);
}}
>
<div>Fullscreen</div>
</FullScreen>
</>
);
}
PS:我不是这个库的作者,但我在我的生产站点中使用它。