Last active
January 31, 2017 14:27
-
-
Save boyney123/1cd4adfbd3466704a4f63050f859fef9 to your computer and use it in GitHub Desktop.
Media Querys in React using CSS and Wrapper Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component, PropTypes } from 'react'; | |
import MediaQuery from './media-query'; | |
class Example extends Component { | |
render() { | |
return ( | |
<div> | |
<MediaQuery size="mobile"> | |
<h1>Mobile View</h1>< | |
</MediaQuery> | |
<MediaQuery size="desktop"> | |
<h1>Desktop View</h1> | |
</MediaQuery> | |
</div> | |
); | |
} | |
} | |
export default Example; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { PropTypes } from 'react' | |
import './styles.scss'; | |
const MediaQuery = ( { size }) => { | |
return ( | |
<div className={`MediaQuery-${size}`}> | |
{ this.props.children } | |
</div> | |
) | |
); | |
MediaQuery.defaultProps = { | |
size: 'mobile' | |
} | |
MediaQuery.propTypes = { | |
size: React.PropTypes.oneOf(['mobile', 'desktop']) | |
} | |
export default MediaQuery; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@media only screen and (min-width: 500px) { | |
.MediaQuery-mobile{ | |
display: none; | |
} | |
} | |
@media only screen and (max-width: 500px) { | |
.MediaQuery-desktop{ | |
display: none; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code link
An error occurred with
this.props.children
I think the best to use. thanks