【问题标题】:I have an error in react native with expo: "syntax error unexpected token" error. Any help to solve it? Thanks我在与 expo 进行本机反应时出错:“语法错误意外令牌”错误。任何帮助解决它?谢谢
【发布时间】:2020-01-02 23:11:31
【问题描述】:

我尝试添加括号或分号,但出现错误:

语法错误:意外的标记

如何实现此功能并使其正常工作?我的完整代码:

还有,为什么不工作?

从'react'导入反应,{组件}; 从'react-native'导入{文本,ActivityIndi​​cator,视图,按钮,TextInput,样式表,TouchableOpacity,尺寸,AppRegistry,平台,状态栏,异步存储}; 导入 { createAppContainer, createSwitchNavigator } from 'react-navigation'; 从'react-navigation-stack'导入{createStackNavigator};

const userInfo = {username: 'admin', password: 'pass12345'} 

class HomeScreen extends Component { 
static navigationOptions = { header: null } 
constructor(props) { 
    super(props);
    this.state = { username: '', password: '' }
}

render() { 
    return ( 
        <View style={styles.container}> 
            <Text>News</Text>
            <TextInput
                style={styles.input} 
                placeholder={'Username'} 
                onChangeText={(username)=>this.setState({username})} 
                value={this.state.username} 
                autoCapitalize="none" 
            /> 
            <TextInput 
                style={styles.input} 
                placeholder={'Password'} 
                secureTextEntry={true} 
                onChangeText={(password)=>this.setState({password})} 
                value={this.state.password} 
            /> 
            <View> 
                <TouchableOpacity 
                    style={styles.btnLogin} 
                    onPress={this._login} 
                    //onPress={() => this.props.navigation.navigate('Details')} 
                >
                    <Text style= {styles.text}>Login</Text> 
                </TouchableOpacity>
                <TouchableOpacity onPress={() => alert("Signup Works")} > 
                    <Text style={styles.btnTxt}>Signup</Text> 
                </TouchableOpacity> 
            </View> 
        </View> 
    ); 
  } 
} 

 class DetailsScreen extends Component { 
render() { 
    return ( 
        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> 
            <Text>Details Screen</Text> 
        </View> 
    ); 
} 
} 

const RootStack = createStackNavigator({ 
//Home: HomeScreen, 
Details: DetailsScreen 
}, {//initialRouteName: 'Home'} ); 

defaultNavigationOptions:{
    headerStyle:{
        backgroundColor: '#1e90ff'
    },
    headerTitleStyle: {
        textAlign:'center',
        flex: 1
        }
     }
},
};



const AuthStack = createStackNavigator({Home: HomeScreen});

class AuthLoadingScreen extends Component {
constructor (props){
    super(props);
    this._loadData();
}

render(){
    return(
        <View>
            <ActivityIndicator/>
            <StatusBar barStyle="default"/>
        </View>    
    );
}
_loadData = async() => {
    const isLogged = await AsyncStorage.getItem('isLoggedIn');
    this.prop.navigation.navigate(isLoggedIn !== '1'? 'Auth':'App');
}
}


export default createAppContainer(
createSwitchNavigator(
  {
    AuthLoading: AuthLoadingScreen,
    App: RootStack,
    Auth: AuthStack,
  },
  {
    initialRouteName: 'AuthLoading',
  }
)
);

_login = async() => { 
    if (userInfo.username === this.state.username && userInfo.password === this.state.password) { 
        //alert('Logged In'); 
        await AsyncStorage.setItem('isLoggedIn', '1'); 
        this.props.navigation.navigate('Details') 
    } else { 
        alert('Username or Password is incorrect.'); 
    } 
 } 


const styles = StyleSheet.create({ 
container:{ flex:1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#00bfff', }, 
btnLogin: { flex: 0.5, height: 45, borderRadius: 25, fontSize: 16, justifyContent: 'center', marginTop: 20 }, 
btnTxt:{ }, 
input:{ justifyContent: 'center', width:"90%", padding: 15, marginBottom: 10 } 
});

Expo Error

【问题讨论】:

  • 附上的图片是expo错误
  • 修复格式和/或使用像 PhpStorm/IntelliJ 这样的 IDE。它会立即指出错误。
  • 我看到了错误,谢谢,但是我该如何解决呢? @mpen
  • 已经有人回复你了。只需修复您不匹配的大括号即可。

标签: reactjs react-native


【解决方案1】:

乍一看,这似乎是个问题:

}, {//initialRouteName: 'Home'} ); 

您已经注释掉了结束} 和结束)

【讨论】:

  • 谢谢!但是我该如何解决呢?
  • 将右大括号和括号移动到下一行,这样它就不会被注释掉?还是使用块注释来禁用 initialRouteName 部分?
猜你喜欢
  • 2019-12-03
  • 2018-03-29
  • 2020-08-28
  • 1970-01-01
  • 2020-08-10
  • 2021-08-06
  • 2011-09-22
  • 2021-12-28
  • 2020-07-24
相关资源
最近更新 更多