Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

media query in ReactJS

Hi all I have following code: my code

As you see I have "mystyle" which have fontSize: 35px and color:"red".

      const mystyle = {
        fontSize: "35px",
        color: "red"
      };

Also I added react-responsive helper to set World component visible when max-width will equal to 600px.

    const isMobile = useMediaQuery({ query: "(max-width: 600px)" });
    return (
      <div>
        <div style={mystyle}> hello </div>
        {isMobile && <World />}
      </div>
   );

Now my question is: How can I change fontSize to 20px and color to "blue" in my "mystyle" when max-width will equal to 600px ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
const mystyle = {
        fontSize: "35px",
        color: "red"
      };

const mystyle2 = {
        fontSize: "20px",
        color: "blue"
      };

    <div style={isMobile ? mystyle2 : mystyle}> hello </div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...