📌  相关文章
📜  反应文件中移动设备的媒体查询 - 任何代码示例

📅  最后修改于: 2022-03-11 14:58:37.182000             🧑  作者: Mango

代码示例1
import React, { Component } from 'react';

class App extends Component {
  constructor(props) {
    super(props)
    this.state = { matches: window.matchMedia("(min-width: 768px)").matches };
  }

  componentDidMount() {
    const handler = e => this.setState({matches: e.matches});
    window.matchMedia("(min-width: 768px)").addListener(handler);
  }
  render() {
    return (
      
{this.state.matches && (

Big Screen

)} {!this.state.matches && (

Small Screen

)}
); } } export default App;