【问题标题】:Pressing a button doesn't load gui按下按钮不会加载 gui
【发布时间】:2017-10-14 20:09:14
【问题描述】:

问题: 所以我正在使用自定义字体编写应用程序,并且我对其进行了编码,因此当您按下按钮时,字体将加载。当您按下显示“15 秒后按我加载!”的按钮时用户输入框应该与一些空帖子、一些文本和另一个按钮(还没有做任何事情)一起出现,但什么都没有显示。我没有收到任何错误。我想知道为什么什么都没有显示,以及如何获得正确的东西来渲染。

工作原理: 当您按下按钮“按我在 15 秒后加载应用程序!”变量 fontLoaded 应该更改为 true (我不知道它是否真的发生了变化),然后这将导致代码渲染所有其他内容以开始。没有渲染。

我还尝试了什么: 我尝试了另一种方法,您将变量 fontLoaded 放在加载字体的代码之后,因此它是自动的,并且也没有任何作用。我没有尝试过其他任何东西,因为我不知道还能尝试什么。

代码:

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 {

  state = {
    fontLoaded: false,
  };

  componentDidMount() {
      Expo.Font.loadAsync({
        'Cabin-Regular-TTF': require('./Cabin-Regular-TTF.ttf'),
      });
 }
  constructor(props) {
    super(props);
    this.state = { postInput: ""}
  }

 render() {
    return (
      <View style={styles.container}>
        <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="#fe8200"
            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>

        <Button
                title="+"
                color="#fe8200"
                accessibilityLabel="Wait 15 seconds and then press me to load the font!"
            />

            <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'}} />
             </ScrollView>
          </View>) : (null) }
      </View>
    );
  }
}

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

【问题讨论】:

    标签: javascript css react-native


    【解决方案1】:

    您应该将this.state.fontLoaded 放在render 方法的返回中,您使用的是未更改的全局变量fontLoaded。希望这会有所帮助!

    【讨论】:

    • 抱歉,我不知道该放在哪里。你能给我一个我必须改变的代码行的例子吗?谢谢!
    • {fontLoaded ? ( &lt;View style={styles.container}&gt; .... 将此变量 fontLoaded 更改为 this.state.fontLoaded
    • 任何时候都很棒!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多