Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum width/height in React Native

Tags:

react-native

How can I set minimum width or height for React Native component?

In css, I can use min-width/min-height

https://facebook.github.io/react-native/docs/flexbox.html#content

There is no minWidth/minHeight on react-native docs

like image 890
SooCheng Koh Avatar asked Oct 30 '15 02:10

SooCheng Koh


People also ask

How do you give min height in React Native?

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 .

How do you use height and width in React Native?

import { Dimensions } from 'react-native'; You can get the application window's width and height using the following code: const windowWidth = Dimensions. get('window').

How do I set the width and height of an image in React Native?

So all we need to do is create a View with width: "100%" , then use onLayout to get the width of that view (i.e. the container width), then use that container width to calculate the height of our image appropriately.

What is the size of React Native?

Migrating to React Native This reduces the size to around 7.5 MB, thin but we can go thinner.


3 Answers

minHeight, maxHeight, minWidth and maxWidth properties are supported as of react-native version 0.29.0 for iOS and Android.

Here is the maxHeight description from react-native documentation. This description also applies for other min/max style properties.

maxHeight is the maximum height for this component, in logical pixels.

It works similarly to max-height in CSS, but in React Native you must use points or percentages. Ems and other units are not supported.

See https://developer.mozilla.org/en-US/docs/Web/CSS/max-height for more details.

A dummy example:

<View
  style={{
    minWidth: '20%',
    maxWidth: 500,
    minHeight: '10%',
    maxHeight: 150,
  }}
/>
like image 176
halilb Avatar answered Oct 05 '22 08:10

halilb


This answer is outdated now, use halilb's answer.

I solved this by using the onLayout prop, its very easy:

Example:

Step 1: I create a prop in our state that will be holding the current height of the image called curImgHeight.

constructor(){
  super(props);
  this.state={curImgHeight:0}
}

Step 2: Use the prop in any View or Element that supports the onLayout prop.

Here I use it with an Image. Then all we have to do is, change that state property whenever the actual image height is than our minimum height.

render(){
  <Image
    source={{uri: "https://placehold.it/350x150"}}
    resizeMode='cover'

    style={[styles.image, {height:(this.state.curImgHeight<=0?null:this.state.curImgHeight)}]}

    onLayout={(e)=>{
      let {height} = e.nativeEvent.layout;
      let minimumImgHeight = 400; //We set the minimum height we want here.
      if(height<= minimumImgHeight){ //Whenever the real height of the image is less than the minimum height
        this.setState({curImgHeight:minimumImgHeight}); //just change the curImgHeight state property to the minimum height.
      }
    }}
  />
}

Thats how I solved it for me.

p.s: During my search I found that react-native unofficially supports minHeight and maxHeight but only for iOS and not for Android. I wouldn't dare using them though. The above code works well and gives me control.

like image 31
SudoPlz Avatar answered Oct 05 '22 08:10

SudoPlz


I was able to work out the solution for you. Here's a working demo... https://rnplay.org/apps/vaD1iA

And here are the key parts.

First, you pull in the device dimensions...

var Dimensions = require('Dimensions');
var {
  width,
  height
} = Dimensions.get('window');

Here's the button component, which uses the device width as the basis for the button's with

const Button = React.createClass({
  render(){
    return(
      <TouchableHighlight>
        <Text style={[styles.button,{width: width - 20}]}>
          {this.props.children}
        </Text>
      </TouchableHighlight>
    )
  }
});

Then, as you can see here, the button width will be the same regardless of label content width.

enter image description here

like image 36
Chris Geirman Avatar answered Oct 05 '22 08:10

Chris Geirman