Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native how to do min/max widths

I want to style a component in my interface. The component must have a width of at least 200, but I want to let it grow with screen width to up to 600. But, sometimes people use tablets or huge phones. And I don't want the component to be able to grow with the screen forever. I want it to have a maximum width of 600.

And I know maxWidth is a thing that is, at least for now, not a part of the flexbox implementation in React Native... so, is there a reasonable way to do this today?

like image 514
chrispitzer Avatar asked Mar 29 '16 20:03

chrispitzer


People also ask

How do you set a max width for a view in React Native?

There is no such thing as "maxWidth" in React Native. You may want to style your component at run-time. Try playing with Dimensions . You can get screen width and screen height of the device and adjust width of your component accordingly.

How do you set the minimum width of a React?

To set the minimum width or height in React Native, we can set the minWidth and minHeight properties. to set the min width with minWidth and min height with minHeight . We can also set the max width with maxWidth and max height with maxHeight .


1 Answers

You can use the maxWidth, maxHeight, minWidth, minHeight layout properties supported by React Native.

Document here React Native layout props.

Example:

StyleSheet.create({
  container: {
    maxWidth: '80%',   // <-- Max width is 80%
    minHeight: 20,     // <-- Min height is 20
  },
});
like image 53
Thai Ha Avatar answered Oct 18 '22 15:10

Thai Ha