Javascript React native-无法呈现作为prop传递的函数中返回的组件
我有一个函数:Javascript React native-无法呈现作为prop传递的函数中返回的组件,javascript,reactjs,react-native,react-native-android,react-native-ios,Javascript,Reactjs,React Native,React Native Android,React Native Ios,我有一个函数: handleSignUp= () => { if(this.state.password === this.state.confirmedPass) { firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).catch((error) => { let errorCode = error
handleSignUp= () => {
if(this.state.password === this.state.confirmedPass) {
firebase.auth().createUserWithEmailAndPassword(this.state.email,
this.state.password).catch((error) => {
let errorCode = error.code;
let errorMessage = error.message;
if (errorCode == 'auth/email-already-in-use') {
return <Custom Component/>
}
else if (errorCode == 'auth/invalid-email') {
alert('invalid email');
return <Custom Component/>
}
else if (errorCode == 'auth/weak-password') {
return <Custom Component/>
}
else {
// ... Some action is executed
}
});
}
你能格式化代码吗?你是否只想在按下按钮时渲染?是的。当的onPress被触发时。你能给出完整的代码吗?@WilliamDarko我不熟悉
react native
,但是如果react关于状态和元素渲染时的相同原则适用,这可能会有所帮助:只渲染从render方法返回的元素,在handleSignup
函数中返回元素,这些元素将被忽略。相反,如果返回元素,则在promise/observable中使用setState(…)
,并在render方法中基于此状态呈现不同的元素。
render() {
return (
<View>
<Btn action={this.handleSignUp}/>
// Btn is a component with prop 'action' which is a function passed to onPress in the Btn definition
</View>
)
}
import React from 'react';
import {
View,
Text,
TouchableNativeFeedback,
TouchableOpacity,
StyleSheet
} from 'react-native';
export default class Btn extends React.Component {
render() {
const btnStyle = this.props.btnStyle;
return(
<TouchableNativeFeedback onPress={this.props.action} background=
{TouchableNativeFeedback.Ripple('#60b0f4',false)}>
<View style={btnStyle}>
<Text style={this.props.textStyle}>{this.props.text}</Text>
</View>
</TouchableNativeFeedback>
);
}
}