2016-08-12 91 views
0

我想在場景之間切換,並測試<{Some imported Scene} />完美的作品,但下面的代碼沒有。 我非常新的反應本地的,我得到的錯誤是在線路30 index.android.js意外的標記,它是甾體抗炎藥this.(scene[route.name] || scene['LoginView']);反應本機導航器意外令牌

index.android.js

import React, { Component } from 'react' 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    TextInput, 
    Navigator, 
    TouchableHighlight 
} from 'react-native'; 

import LoginView from './Scence-Android/LoginView'; 
import SettingView from './Scence-Android/SettingView'; 

class TrackrModule extends Component { 
    render() { 
    return (
     <View> 
     <Navigator 
      initialRoute={{ name: 'LoginView' }} 
      renderScene={ (route, navigator) => { 
      var scene = { 
       "LoginView":() => { 
       return (<LoginView navigator={navigator} title="Login" />) 
       }, 
       "SettingView":() => { 
       return (<SettingView navigator={navigator} title="Setting" />) 
       } 
      } 
      this.(scene[route.name] || scene['LoginView']); 
      }} 
     /> 
     </View> 
    ); 
    } 
} 

LoginView

import React, {Component} from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    TextInput, 
    Navigator, 
    TouchableHighlight 
} from 'react-native'; 

export default class LoginView extends Component { 
    constructor(props){ 
    super(props); 
    this.state = { 
     username: "", 
     password: "" 
    }; 
    } 
    btnLogin_pressed(){ 
    this.props.navigator.push({ 
     name:"SettingView" 
    }); 
    } 

    render(){ 
    return (
     <View style={styles.container}> 
     <View style={styles.loginContainer}> 
      <TextInput> 
      placeholder="Username" 
      style={styles.textInput} 
      onChange={(event) => this.setState({username: event.nativeEvent.text})} 
      value={this.state.username} 
      </TextInput> 

      <TextInput> 
      placeholder="Password" 
      secureTextEntry = { true } 
      style={styles.textInput} 
      onChange={(event) => this.setState({password: event.nativeEvent.text})} 
      value={this.state.password}/> 
      </TextInput> 

      <TouchableHighlight onPress={this.btnLogin_pressed.bind(this)}> 
      <Text>Login</Text> 
      </TouchableHighlight> 
     </View> 
     </View> 
    ) 
    } 
} 

回答

1

在JavaScript之後,你不能使用a()運算符。所以this.(scene[route.name] || scene['LoginView']);給你一個錯誤。

您還需要從renderScene方法返回一個組件。所以,下面的代碼將超過this.(scene[route.name] || scene['LoginView']);更合適:

return (scene[route.name] || scene['LoginView']);

+0

也許'回報(場景[route.name || 'LoginView'])'? – stereodenis

+0

我認爲場景是一個以路線名稱作爲關鍵字和組件作爲值的對象。所以'return(scene [route.name] || scene ['LoginView']);'應該是正確的。 @stereodenis –

+0

謝謝@AakashSigdel和stereodenis,那是一個醉酒的編碼:) – XPLOT1ON