0
我有一個我正在開發的反應本機應用程序。現在我希望它有兩個意見。登錄頁面和主頁面。它到目前爲止是這樣的:React Native - 有條件地呈現視圖
index.ios.js
import React, { Component } from 'react';
import {
AppRegistry,
Dimensions,
StyleSheet,
Text,
TextInput,
Button,
TouchableHighlight,
View
} from 'react-native';
import Scanner from './app/components/Scanner';
import Login from './app/components/Login';
import Store from './app/mobx/store';
export default class TestApp extends Component {
render() {
if (Store.loggedIn()){
return <Scanner />
} else {
return <Login store={Store} />
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
backgroundColor: '#114DA0'
}
});
AppRegistry.registerComponent('TestApp',() => TestApp);
應用程序/組件/ Login.js
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
ScrollView
} from 'react-native';
import Container from './Container';
import Button from './Button';
import Label from './Label';
import {observer} from 'mobx-react/native';
@observer
export default class Login extends Component {
constructor() {
super();
this.state = {
email: '',
password: '',
isLoggingIn: false,
message: ''
}
this._userLogin = this._userLogin.bind(this);
}
_userLogin() {
console.log("loggin in...")
this.setState({isLoggingIn: true, message:''});
var params = {
email: this.state.email,
password: this.state.password
};
var formBody = [];
for (var property in params) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(params[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
fetch("http://example.com:8000/auth/mobile_login", {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formBody
})
.then((response) => {
return response.json()
})
.then((response) => {
if (response.error) {
this.setState({message: response.message});
} else if (response.user) {
this.props.store.logIn(response.user);
}
})
.then(() => {
this.setState({isLoggingIn: false})
})
.done();
}
render() {
return (
<ScrollView style={styles.scroll}>
<Container>
<Label text="Email" />
<TextInput
style={styles.textInput}
onChangeText={(email) => this.setState({email})}
/>
</Container>
<Container>
<Label text="Password" />
<TextInput
secureTextEntry={true}
style={styles.textInput}
onChangeText={(password) => this.setState({password})}
/>
</Container>
<Container>
{!!this.state.message && (
<Text style={{fontSize: 14, color: 'red', padding: 5}}>
{this.state.message}
</Text>
)}
<Button
label="Sign In"
styles={{button: styles.primaryButton, label: styles.buttonWhiteText}}
onPress={this._userLogin} />
</Container>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
...
});
應用程序/組件/ Scanner.js
import React, { Component } from 'react';
import {
AppRegistry,
Dimensions,
StyleSheet,
Text,
TouchableHighlight,
View
} from 'react-native';
import Camera from 'react-native-camera';
export default class Scanner extends Component {
constructor(){
super();
this.onBarCodeRead = this.onBarCodeRead.bind(this);
}
onBarCodeRead(e) {
this.setState({text: e.data});
}
render() {
return (
<View style={styles.container}>
<Camera
ref={(cam) => {
this.camera = cam;
}}
onBarCodeRead={this.onBarCodeRead}
style={styles.preview}
aspect={Camera.constants.Aspect.fill}>
</Camera>
</View>
);
}
}
const styles = StyleSheet.create({
...
});
AppRegistry.registerComponent('Scanner',() => Scanner);
這兩個頁面都可以正常工作,我可以通過登錄頁面登錄到我的服務。但是,一旦我登錄,我怎樣才能讓根組件呈現掃描器頁面?