【发布时间】:2019-12-01 13:29:54
【问题描述】:
我正在尝试创建一个登录模块。我有一个 LoginView,它定义了视图和一个 LoginController,我在其中定义了所有用户交互。现在我正在尝试合并一个逻辑,在该逻辑中,LoginController 将更改 LoginView 的状态,例如将 isLoading 的值从 false 更改为 true,以防所有输入数据都有效
登录查看
import React, { Component, Fragment} from 'react';
import LoginController from '../Controller/LoginController.js';
import {
View,
ScrollView,
StatusBar,
SafeAreaView,
TextInput,
TouchableOpacity,
Text,
StyleSheet
} from 'react-native';
const styles = StyleSheet.create({
container: {
paddingTop: 23
},
input: {
margin: 15,
height: 40,
borderColor: '#7a42f4',
borderWidth: 1
},
submitButton: {
backgroundColor: '#7a42f4',
padding: 10,
margin: 15,
height: 40,
},
submitButtonText:{
color: 'white'
}
});
export default class LoginView extends Component {
constructor(){
super()
this.state = {
isLoading: false
}
}
changeLoadingState = (currentLoadingState) => {
/* Create a loader screen and incorporate it here.
*/
this.setState({isLoading: currentLoadingState} , () => {
console.log("This is called when this.setState has resolved");
console.log(this.state.isLoading);
});
}
render() {
const con = new LoginController(this.changeLoadingState);
return (
<Fragment>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<View style = {styles.container}>
<TextInput style = {styles.input}
underlineColorAndroid = "transparent"
placeholder = "Email"
placeholderTextColor = "#9a73ef"
autoCapitalize = "none"
onChangeText = {con.handleEmail}/>
<TextInput style = {styles.input}
underlineColorAndroid = "transparent"
placeholder = "Password"
placeholderTextColor = "#9a73ef"
autoCapitalize = "none"
onChangeText = {con.handlePassword}/>
<TouchableOpacity
style = {styles.submitButton}
onPress = {
() => con.login()
}>
<Text style = {styles.submitButtonText}> Submit </Text>
</TouchableOpacity>
</View>
</SafeAreaView>
</Fragment>
);
}
}
LoginController.js
import React, { Component } from 'react';
import LoginNetworkManager from '../NetworkManager/LoginNetworkManager.js';
import Loader from '../../Utils/Loader.js';
export default class LoginController extends Component {
constructor(props) {
super(props);
this.state = {
email: null,
password: null
};
this.changeLoadingState = this.changeLoadingState.bind(this);
}
changeLoadingState = (currentLoadingState) => {
this.props.changeLoadingState(currentLoadingState);
}
handleEmail = (text) => {
this.setState({email: text});
}
handlePassword = (text) => {
this.setState({password: text});
}
login = () => {
this.changeLoadingState(this.validate());
if (this.validate() == true) {
// Here in we will call the API
} else {
console.log(" It's false ");
// Do nothing
}
}
validate = () => {
var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var isValid = reg.test(this.email);
if (isValid) {
isValid = (this.password.trim().length > 0);
}
console.log(" Tis is Valid " + isValid);
return isValid
}
}
点击登录按钮时的错误是
_this.props.changeLoadingState is not a function
handleException @ ExceptionsManager.js:86
handleError @ setUpErrorHandling.js:23
reportFatalError @ error-guard.js:42
__guard @ MessageQueue.js:345
callFunctionReturnFlushedQueue @ MessageQueue.js:105
(anonymous) @ debuggerWorker.js:80
【问题讨论】:
-
道具不是这样工作的。你的
LoginController不是一个组件,或者至少它不像一个组件那样使用。 -
我使用组件是因为我想将参数作为构造函数注入发送。基本上这个想法是 LoginView 将调用 LoginController 来处理它的用户交互,任何与 UI 相关的更改都将在 LoginView 类中完成。这样,逻辑就分开了。但是,如果有其他逻辑可以用来实现相同的理想,请告诉我
标签: reactjs react-native components react-props