如果语句:SyntaxError预期的意外标记,

时间:2017-10-11 00:27:42

标签: javascript react-native

在本机反应中,我试图放一些代码,按下按钮后会加载字体。 if语句出现错误(第38:6行),表示意外的令牌,预期,(38:6)。我不知道为什么会发生这种情况,我不知道在哪里放置它想要的逗号,或者问题是否是其他问题。

What happens between uploading a file to an HTML form and submitting it?

error message

更新的代码:

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 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) }
    );
  }


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

3 个答案:

答案 0 :(得分:0)

尝试

{
  fontLoaded && (
    <View style={styles.container}>
      ....
    </View>
  )
}

我也在你的onPress中看到问题 - 它应该是

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

答案 1 :(得分:0)

组件的模板必须只有一个顶级容器。所以我把你的组件放在一个封闭的标签之间,错误消失了:

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'
  },
});

此外,您在课程定义结束时错过了结束}。我用评论标记了它。

答案 2 :(得分:0)

为什么不重构代码并将所有scrollView代码移动到新函数, e.g:

 renderScrollWrapper() {
   retun(
   <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>);
}

然后将渲染方法更改为,

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 ? {this.renderScrollWrapper()} : (null) }
  </View>
);

}