
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-native-media-query
Advanced tools
Write once, use anywhere:
Media queries for react-native and react-native-web
Adds support for media queries in react-native and react-native-web, which allows you to reuse styles between different platforms and form-factors, such as web, smart tvs, mobiles, tablets and native tvs.
This package also works with next.js static generation or server-side rendering. Since react-native-media-query
creates css based media queries on web, it allows you to easily avoid flash of unstyled (or incorrectly styled) content on initial load.
:hover
and other pseudo classes should also work on web and other web based platforms, such as tizen, webOS and more.
To trigger media queries on device orientation changes for native platforms, for now you will most likely need to update the state, because react-native-media-query
has no listeners inside. Simple useWindowDimensions
from react-native
, or similar should work.
yarn add react-native-media-query
or
npm install react-native-media-query --save
import StyleSheet from 'react-native-media-query';
import { View, TextInput } from 'react-native'
const {ids, styles} = StyleSheet.create({
example: {
width: 100,
height: 100,
backgroundColor: 'green',
'@media (max-width: 1600px) and (min-width: 800px)': {
backgroundColor: 'red',
},
'@media (max-width: 800px)': {
backgroundColor: 'blue',
},
},
exampleTextInput: {
paddingVertical: 27,
backgroundColor: 'pink',
'@media (max-width: 768px)': {
paddingVertical: 13,
},
// example CSS selectors. these only work on web based platforms
'::placeholder': {
color: 'green',
},
':hover': {
backgroundColor: 'red',
},
}
})
...
// dataSet is only required for web
// for react-native-web 0.13+
<View style={styles.example} dataSet={{ media: ids.example }} />
<TextInput
style={styles.exampleTextInput}
dataSet={{ media: ids.exampleTextInput }}
placeholder="Search"
...
/>
// for older react-native-web
<View style={styles.example} data-media={ids.example} />
// or if you want to use HTML tags for web only, it will also work
<div style={styles.example} data-media={ids.example} />
Update your custom document (pages/_document.js)
like in example below. Further usage is exactly the same as shown above.
import Document, { Html, Head, Main, NextScript } from 'next/document';
import React from 'react';
import { flush } from 'react-native-media-query';
import { AppRegistry } from 'react-native-web';
export default class CustomDocument extends Document {
static async getInitialProps({ renderPage }) {
AppRegistry.registerComponent('Main', () => Main);
const { getStyleElement } = AppRegistry.getApplication('Main');
const { html, head } = renderPage();
const styles = [ getStyleElement(), flush() ];
return { html, head, styles: React.Children.toArray(styles) };
}
render(){
...
}
FAQs
Media queries for react-native and react-native-web
The npm package react-native-media-query receives a total of 4,289 weekly downloads. As such, react-native-media-query popularity was classified as popular.
We found that react-native-media-query demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.