语法错误意外令牌,反应原生

时间:2018-06-12 19:04:12

标签: javascript react-native react-native-ios

我有一个Login屏幕。当我按下蓝色文本时,我希望它导航到Register屏幕。 但是,它始终显示相同的Syntax Error: Unexpected Token

import React, { Component } from 'react';
import {
  ScrollView,
  Text,
  TextInput,
  View,
  Button
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import Register from './src/screens/Register';

export default class Login extends Component {
static navigationOptions = {
  title: 'Welcome',
}

navigateToRegister = () => {
  this.props.navigation.navigate('Register');
}

render() {
  return (
      <ScrollView style={{padding: 20}}>
          <Text
              style={{fontSize: 27}}>
              Login
          </Text>
          <TextInput placeholder='Username' />
          <TextInput placeholder='Password' />
          <View style={{margin:7}} />
          <Button
                  onPress={this.props.onLoginPress}
                  title="Submit"
              />
          <Text style={{color: 'blue'}}
              onPress={ this._navigateToRegister }
              Register
          </Text>
          </ScrollView>
      );

}
}
const App = StackNavigator({
  Login: { screen: Login },
  Register: { screen: Register },
  Secured: { screen: Secured },
});

AppRegistry.registerComponent('App', () => App);

2 个答案:

答案 0 :(得分:1)

您在代码中遇到语法错误。

  

倒数第二个文字没有结束尖括号>

import React, { Component } from "react";
import { ScrollView, Text, TextInput, View, Button } from "react-native";
import { StackNavigator } from "react-navigation";
import Register from "./src/screens/Register";

export default class Login extends Component {
  static navigationOptions = {
    title: "Welcome"
  };

  navigateToRegister = () => {
    this.props.navigation.navigate("Register");
  };

  render() {
    return (
      <ScrollView style={{ padding: 20 }}>
        <Text style={{ fontSize: 27 }}>Login</Text>
        <TextInput placeholder="Username" />
        <TextInput placeholder="Password" />
        <View style={{ margin: 7 }} />
        <Button onPress={this.props.onLoginPress} title="Submit" />
        <Text
          style={{ color: "blue" }}
          onPress={this._navigateToRegister}
          Register
        />
      </ScrollView>
    );
  }
}
const App = StackNavigator({
  Login: { screen: Login },
  Register: { screen: Register },
  Secured: { screen: Secured }
});

AppRegistry.registerComponent("App", () => App);
  

你可以设置更漂亮和eslint他们将为你提供语法错误直白编辑代码,这样你就不需要花时间找到这种错误。   This is good guide to setup everything

答案 1 :(得分:0)

我遇到了代码中的错误

  1. 您已在onPress中使用this._navigateToRegister函数,并将navigateToRegister定义为函数。

  2. 您忘记关闭文字标记

  3. /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            System.out.println( "Hello World!" );
        }
    }