Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

media query in react js

import React, { useState, useEffect } from 'react';

const App = () => {
  const [matches, setMatches] = useState(
    window.matchMedia("(min-width: 768px)").matches
  )

  useEffect(() => {
    window
    .matchMedia("(min-width: 768px)")
    .addEventListener('change', e => setMatches( e.matches ));
  }, []);

  return (
    <div >
      {matches && (<h1>Big Screen</h1>)}
      {!matches && (<h3>Small Screen</h3>)}
    </div>
  );
}

export default App;
Comment

react use media query

//i used bootstrap carousel where i dynamically changed its height for mobile.
import Carousel from 'react-bootstrap/Carousel';
import 'bootstrap/dist/css/bootstrap.min.css';
import {useState, useEffect} from 'react';

function MyCarousel() {

  const [carouselHeight,setCarouselHeight] = useState("500px");

  const img_style = { //style constant used to set carousel height below
      height: carouselHeight,
  }

  useEffect( () => {  //this useEffect Hook will trigger only once when this component is mounted (loaded) to DOM
      if(window.innerWidth <= 382){
        setCarouselHeight("200px");
      }
  })

  return (
    <Carousel fade style={img_style}>
      <Carousel.Item>
        <img
          style={img_style}
          className="d-block w-100"
          src="images/carousel1.jpg"
          alt="First slide"
        />
        <Carousel.Caption>
          <h3>First slide label</h3>
          <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
        </Carousel.Caption>
      </Carousel.Item>
      <Carousel.Item>
        <img
          style={img_style}
          className="d-block w-100"
          src="images/carousel2.jpg"
          alt="Second slide"
        />

        <Carousel.Caption>
          <h3>Second slide label</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        </Carousel.Caption>
      </Carousel.Item>
      <Carousel.Item>
        <img
          style={img_style}
          className="d-block w-100"
          src="images/carousel3.jpg"
          alt="Third slide"
        />

        <Carousel.Caption>
          <h3>Third slide label</h3>
          <p>
            Praesent commodo cursus magna, vel scelerisque nisl consectetur.
          </p>
        </Carousel.Caption>
      </Carousel.Item>
    </Carousel>
  );
}

export default MyCarousel;
Comment

PREVIOUS NEXT
Code Example
Javascript :: access multiple classes with loop 
Javascript :: password validation regex 
Javascript :: recoil js 
Javascript :: display object in array 
Javascript :: React ES6 Arrow Functions 
Javascript :: react svg 
Javascript :: acer swift 5 
Javascript :: LocomotiveScroll npm 
Javascript :: angular http post example 
Javascript :: warning each child in a list should have a unique key prop does not disappear 
Javascript :: visual studio node.js cleint missing intents error 
Javascript :: avoid compressing imagepicker react native 
Javascript :: javascript function to sleep 
Javascript :: json with postgresql 
Javascript :: aframe basic example 
Javascript :: find outlier js 
Javascript :: beanstalk nodejs default port 
Javascript :: merge two binary trees 
Javascript :: Get Input arrays 
Javascript :: hardhat async test 
Javascript :: node_modules/react-native-paper/lib/module/core/Provider.js 
Javascript :: $_GET data using javascript 
Javascript :: getx navigation 
Javascript :: sharepoint javascript get current user 
Javascript :: string.length 
Javascript :: js how to sort array by object value 
Javascript :: json schema example 
Javascript :: timer in angular 8 
Javascript :: add points to numbers js 
Javascript :: remove elemtns from an array with splice 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =