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

media query in jsx

window.matchMedia("(min-width: 768px)").matches
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 :: combine two arrays javascript 
Javascript :: javascript array find highest value of array of objects by key 
Javascript :: calculate string value in javascript, not using eval 
Javascript :: sorting number with coma datatable 
Javascript :: unexpected token export type react bottontab navigation 
Javascript :: get value of checked radio button jquery 
Javascript :: hook access loopback 
Javascript :: html form pattern message 
Javascript :: vim react snippets 
Javascript :: javascript check if var is string 
Javascript :: node readline question 
Javascript :: page reload button using angular 
Javascript :: javascript set localstorage 
Javascript :: how to remove a part of a string javascript 
Javascript :: js format urcurency 
Javascript :: jquery innerhtml 
Javascript :: window location redirect javascript 
Javascript :: vehicle number regex 
Javascript :: how to use await to console 
Javascript :: URL scheme "localhost" is not supported. 
Javascript :: clear canvas 
Javascript :: Javascript prime number check 
Javascript :: js check which number is larger 
Javascript :: javascript array remove empty strings 
Javascript :: json schema string or null 
Javascript :: js not equal to null 
Javascript :: extract number from string 
Javascript :: get middle of string js 
Javascript :: for each element in object 
Javascript :: return first letter of string javascript in uppercase 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =