如何使本机Web充分发挥作用?

amo*_*new 7 react-native react-native-flexbox react-native-web

我正在使用React Native Web库,但是我无法在Web上全高

码:

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;
Run Code Online (Sandbox Code Playgroud)

chrome浏览器的结果: 在此处输入图片说明

Raj*_*dar 8

React Native 不理解视口单位。我们可以使用Platformreact-native提供的API。

如果您在使用react-native-web时想要移动设备和网络的全高,请尝试此方法。

<View style={styles.fullHeight}>
  <Text>Hey there</Text>
</View>
Run Code Online (Sandbox Code Playgroud)

CSS代码

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

或者

const styles = StyleSheet.create({
   fullHeight: {
     ...Platform.select({web: {height: '100vh'}, default: {height: '100%'}})
   }
})
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案比 Dimensions.get("window").height 解决方案更好,因为它在调整浏览器窗口大小时起作用。 (2认同)

amo*_*new 5

什么解决我的问题是位置属性

position:'absolute' 或者 position:'fixed'

亲戚不工作 position:'relative'