how to make react native web full height?
Asked Answered
H

8

10

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

Hawsepipe answered 26/12, 2017 at 9:21 Comment(0)
H
6

what fix my problem is the position property

position:'absolute' or position:'fixed'

relative not working position:'relative'

Hawsepipe answered 26/12, 2017 at 11:18 Comment(0)
N
8

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%'}})
   }
})
Novara answered 22/10, 2019 at 18:58 Comment(1)
This solution is better than the Dimensions.get("window").height solution because it works when the browser window is resized.Subminiature
H
6

what fix my problem is the position property

position:'absolute' or position:'fixed'

relative not working position:'relative'

Hawsepipe answered 26/12, 2017 at 11:18 Comment(0)
G
4

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.

Griddle answered 10/3, 2019 at 9:41 Comment(0)
L
1

When using react-native-web, you should wrap a root-level tree with a viewport shim:

    <View
      style={{
        height: Dimensions.get('window').height,
        width: Dimensions.get('window').width,
      }}
    >
      ...
    </View>

This will normalize the abstract units across platform and allow all children to correctly respond to measured flex (including correct measurements from the onLayout eventing)

ProTip: you should subscribe your layout sizes to Dimensions.addEventListener('change',...) to capture reorientations.

Lysimachus answered 10/9, 2020 at 10:20 Comment(0)
F
1

Adding this style on container of my App file works for me Acctully:

const styles = StyleSheet.create({
  root: {
    flex:1,
    ...Platform.select({web: {height: '100vh'}}),
  },
});

App.js look like this:

...
return(<View style={styles.root}>
...
Flimsy answered 18/12, 2020 at 11:33 Comment(0)
N
0

Try the following

 container: {
        flex: 1
        width: '100%',
        height: '100%',
        margin: 0,
        padding: 0,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'red'
    }
Nanete answered 26/12, 2017 at 10:16 Comment(0)
T
0

You can use Viewports Units for that. If you want them on react-native you need to:

npm install react-native-viewport-units --save

https://www.npmjs.com/package/react-native-viewport-units

With that you can do.

var {vw, vh, vmin, vmax} = require('react-native-viewport-units');

<View style={{width:100*vw}}/>
Therianthropic answered 10/3, 2019 at 9:58 Comment(1)
I hope this is cross platformGruelling
A
0

I am using react native web for a web app. It has left permanent sidebar and the content on the right is scrollable.

I have achieved this by adding ScrollView container to each component in Drawer.Screen.

Then I added a View container to the Drawer and added Dimensions.get("window").height as height to it.

Sidebar navigation created by using drawer navigation.

Hope this helps someone, took me a while to set it up correctly.

Adding a screenshot from the actual interface

enter image description here

import {Dimensions, ScrollView, Text, View} from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';

// Get window height to make screens scrollable
const windowHeight = Dimensions.get('window').height;

function Feed() {
  return (
    <ScrollView>
      <Text>Feed Screen</Text>
    </ScrollView>
  );
}

function Notifications() {
  return (
    <ScrollView>
      <Text>Notifications Screen</Text>
    </ScrollView>
  );
}

const Drawer = createDrawerNavigator();

function MyDrawer() {
  return (
    <Drawer.Navigator initialRouteName="Feed" drawerType="permanent">
      <Drawer.Screen
        name="Feed"
        component={Feed}
        options={{ drawerLabel: 'Home' }}
      />
      <Drawer.Screen
        name="Notifications"
        component={Notifications}
        options={{ drawerLabel: 'Updates' }}
      />
    </Drawer.Navigator>
  );
}
<View style={{height: windowHeight}}>
  <MyDrawer />
</View>
Aron answered 26/3, 2021 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.