Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make react native web full height?

I am using react native web library but I can't make full height on web

Code:

import React from 'react';
import {StyleSheet, View, Text, TouchableOpacity} from 'react-native';

class index extends React.Component {

    render() {
        return (
            <View style={styles.container}>

                <TouchableOpacity
                    style={{
                    backgroundColor: 'blue'
                }}>
                    <Text style={{
                        color: 'green'
                    }}>Learning</Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        width: '100%',
        height: '100%',
        margin: 0,
        padding: 0,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'red'
    }
})

export default index;

The result of chrome browser: enter image description here

like image 799
amorenew Avatar asked Dec 26 '17 09:12

amorenew


People also ask

How do you get full height 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 get content height in React Native Webview?

You can get the content height by injecting the JS code as suggested by @ken-ratanachai-s. Although, You will experience certain irregularities in some devices (Extra height after the content). This is becuase the javascript returns the content height in pixels, but we need to use display points in react native.


3 Answers

what fix my problem is the position property

position:'absolute' or position:'fixed'

relative not working position:'relative'

like image 109
amorenew Avatar answered Nov 05 '22 01:11

amorenew


React native does not understand viewport units. We can use Platform API provided by react-native.

If you want full height for mobile as well as a web while using react-native-web try this method.

<View style={styles.fullHeight}>
  <Text>Hey there</Text>
</View>

CSS code

const styles = StyleSheet.create({
  fullHeight: {
    height: Platform.OS === 'web' ? '100vh' : '100%'
  }
})

or

const styles = StyleSheet.create({
   fullHeight: {
     ...Platform.select({web: {height: '100vh'}, default: {height: '100%'}})
   }
})
like image 33
Rajendran Nadar Avatar answered Nov 05 '22 00:11

Rajendran Nadar


flex: 1 and height: "100%" work fine for mobile version of react-native-web but not for web. Dimensions.get("window").height - work good for both versions.

like image 4
bigcheeseh Avatar answered Nov 05 '22 01:11

bigcheeseh