0

I am trying to responsive my design. My logic is when the div style will be display:"block" it will show a basket and when the user will browse on mobile the div display will be none and the user need to click on the button to open the basket.

Now the problem is my default state is true that is why when a mobile user visits the website he will see first a basket that I don't want. I want when the user will first visit he will not see the basket he needs to click on the button. I can't set the default state to false because the desktop user can't see the basket.

// MY STATE
  const [toggle, setToggle] = useState(true);

<button onClick={() => setToggle(!toggle)}>Open</button>
<div
  className="box_order mobile_fixed"
  style={{ display: `${toggle ? "block" : "none"}` }}
  >
// BASKET CODE
</div>

4 Answers 4

1

You could use window.matchMedia to set the initial value of the state.

The Window interface's matchMedia() method returns a new MediaQueryList object that can then be used to determine if the document matches the media query string, as well as to monitor the document to detect when it matches (or stops matching) that media query.

For instance:

const [toggle, setToggle] = useState(window.matchMedia("max-width: 768px").matches);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but when I set the matchMedia query it is working on the mobile but it's not working on the desktop. now by default, the setToggle is false that's why the conditional class is displayed none for the desktop.
@Jerin, i think because you resize the browser window , if you show your page in desktop dimensions and hit refresh you will see it works.
0

You can set the default value of toggle according to screen size with the media queries api. And listen for screen size change I you need.

Please take a look to this response: Media query syntax for Reactjs

Comments

0

Are you using tailwindcss? follow this to add tailwind to your project through CDN https://tailwindcss.com/docs/installation/play-cdn.

Refactor your className like ${toggle ? "hidden" : "block"} sm:block and remove your style property. In mobile view, the div will always be hidden. It won't display it as a block because the media query sm:block will only display it as a block when the screensize reaches 768 width & more. This will only display the basket once you've click the button. Also use this convention for your setToggle function setToggle(prevState => !prevState).

3 Comments

I am using bootstrap:(
If that's the case, try className=md-block ${toggle ? "d-none" : "d-block"}. Don't forget to remove the style property
my bad, top one doesn't work ${toggle ? "d-none d-md-block" : "d-block d-md-none"}.
0

Set the default state to false and use useState callback

// MY STATE
  const [toggle, setToggle] = useState(window.innerWidth > 700);

<button onClick={() => setToggle(toggleBefore => !toggleBefore)}>Open</button>
<div
  className="box_order mobile_fixed"
  style={{ display: toggle ? "block" : "none" }}
  >
// BASKET CODE
</div>

1 Comment

Not working, :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.