【问题标题】:In React Native, nothing renders with the code I have在 React Native 中,我所拥有的代码不会呈现任何内容
【发布时间】:2017-10-12 16:45:13
【问题描述】:

所以我很困惑,因为在下面的代码中,一个按钮应该呈现,但什么都没有。我得到的只是一个空白的白色屏幕。我没有收到任何错误,终端说已完成加载 javaScript 包,因此代码可以正常工作。我不知道为什么会这样,我真的很想得到一些帮助。

这是使用 react native 和 javascript。

import React, { Component } from 'react';
import { StyleSheet, Text, View, Image, TextInput, ScrollView, TouchableHighlight, Button } from 'react-native';
import { Font } from 'expo';

var fontLoaded = false;

export default class App extends React.Component {

  componentDidMount() {
      Expo.Font.loadAsync({
        'Cabin-Regular-TTF': require('./Cabin-Regular-TTF.ttf'),
      });


  }
  constructor(props) {
    super(props);
    this.state = { postInput: ""}
  }

 render() {
    return (
      <View>
        <View style={styles.container}>
          <View style={{width: 1, height: 30, backgroundColor: '#e8e8e8'}} />
          <Button
            onPress={this.setState({ fontLoaded: true })}
            title="Press Me To Load the App After 15 Seconds!"
            color="#841584"
            accessibilityLabel="Wait 15 seconds and then press me to load the font!"
          />
        </View>


        {fontLoaded ? (
          <View style={styles.container}>
            <Text style={{ fontFamily: 'Cabin-Regular-TTF', fontSize: 16 }}>
                Whats on your mind? Create a post!
            </Text>  

            <TextInput>
                style={{height:40, width: 320, borderColor: '#303030', borderWidth: 1}}
                onChangeText={(postInput)=>this.setState({postInput})}
                value={this.state.postInput}    
            </TextInput>

            <ScrollView>
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 300, height: 250, backgroundColor: '#1daff1'}} />
               <View style={{width: 300, height: 40, backgroundColor: '#147aa8'}} />
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 300, height: 250, backgroundColor: '#1daff1'}} />
               <View style={{width: 300, height: 40, backgroundColor: '#147aa8'}} />
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 300, height: 250, backgroundColor: '#1daff1'}} />
               <View style={{width: 300, height: 40, backgroundColor: '#147aa8'}} />
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 300, height: 250, backgroundColor: '#1daff1'}} />
               <View style={{width: 300, height: 40, backgroundColor: '#147aa8'}} />
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
             </ScrollView>
          </View>) : (null) }
      </View>
    );
  }

} // missing one!

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#e8e8e8',
    alignItems: 'center',
    justifyContent: 'center'
  },
});

【问题讨论】:

  • 您是否尝试过返回一个容器+其中的某些内容,而不是在您的三元中返回 null,以便您可以查看是否是 'fontLoaded' 的问题?

标签: javascript react-native rendering


【解决方案1】:

首先,我认为您有样式问题。最外面的View 至少应该有一个flex:1 样式的道具。删除justifyContent: 'center'alignItems: 'center' 也会有所帮助。

您的第二个问题是由ButtononPress 属性引起的。您正在执行该函数,而不是将其作为参数传递。

应该像下面这样;

onPress={() => this.setState({ fontLoaded: true })}

【讨论】:

  • 对不起,我很困惑。我在哪里放 flex:1?我还更新了新闻代码
  • fontLoaded 应该是this.state.fontLoaded
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 2019-10-22
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多