저는 react-native
을 처음 사용하지만 간단한 로그인 UI를 만드는 것으로 놀고 있습니다. 로그인 구성 요소가 있고 loginForm
및 forgotPasswordForm
에 대한 두 개의 개별 폼 구성 요소가 있습니다. 내 로그인 구성 요소에 React 네이티브 - 함수 호출
state
기반으로 할 것입니다 가정합니다
loginForm
또는
forgotPasswordForm
을 보여 여부를 결정하려고하는
renderForm
기능을 가지고있다.
로그인 구성 요소 :
export default class LoginForm extends Component {
forgotPasswordForm(){
// Thought I could setState here so that the loginComponent would update and see the state and render the forgotPasswordForm instead
}
render() {
return (
<View style={styles.container}>
<StatusBar
barStyle="light-content"
/>
<TextInput
placeholder="username or email"
placeholderTextColor="rgba(255,255,255,0.7)"
returnKeyType="next"
onSubmitEditing={() => this.passwordInput.focus()}
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
style={styles.input}
/>
<TextInput
placeholder="password"
placeholderTextColor="rgba(255,255,255,0.7)"
secureTextEntry={true}
returnKeyType="go"
style={styles.input}
ref={(input) => this.passwordInput = input}
/>
<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>LOGIN</Text>
</TouchableOpacity>
<View style={styles.forgotPasswordContainer}>
<Text style={styles.forgotPasswordText}>Trouble logging in? </Text>
<TouchableOpacity onPress={this.forgotPasswordForm()}>
<Text style={styles.activeLink}>Click Here.</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
는이 코드의 일부는 있어야 할 곳에에서 조금 혼동 될 수있다 : 여기
export default class Login extends Component {
state = { 'display': '' };
// Render the content
renderForm(){
// What page should show?
switch(this.state.display){
case 'forgotPasswordForm':
return <ForgotPassword />;
break;
case 'loginForm':
return <LoginForm />;
break;
default:
return <LoginForm />;
break;
}
}
render() {
return (
<KeyboardAvoidingView behavior="padding" style={styles.container}>
<View style={styles.logoContainer}>
<Image
style={styles.logo}
source={require('../../images/logo.png')}
/>
<Text style={styles.logoText}>Behavior Tracking System</Text>
</View>
<View style={styles.formContainer}>
{this.renderForm()}
</View>
</KeyboardAvoidingView>
);
}
}
는 forgotPasswordFunction
에 대한 링크가 포함되어 내 LoginForm
입니다 놓았다. 나는 LoginComponent가 폼 필드 자체를 포함하고 있다고 가정하기 때문에, 우리는 loginForm
또는 forgotPasswordForm
을 보여줄지를 결정하기 위해 스위치 로직을 넣을 곳을 가정한다.
내 문제는 forgotPassword 링크에 대해 loginForm
에있는 onClick
입니다. 양식을 전환하기 위해 로그인 구성 요소를 업데이트하는 방법을 모르는 경우.
내 목표는 '여기를 클릭하십시오 "링크를 눌렀을 때, 대신 로그인 필드의 비밀 번호 복구 필드를로드하는 것입니다.
덕분에 답장을 그 변경 한 후, 나는 다음과 같은 오류가 나타날 수 있습니다 기존의 상태 등 render''내 전이 (또는 다른 구성 요소의 생성자 동안 업데이트 할 수 없습니다) ". 페이지는 로그인 대신 암호 분실 양식으로 기본 설정됩니다. 어쩌면 내가 다른 걸 놓친 것일까? – SBB
나는 this.state에서 'loginForm'으로 디스플레이를 편집했고, 내가 한 모든 변경을 했습니까? renderForm() {}에서 renderForm =() => {}으로 함수를 작성하는 방법을 변경했습니다. –
onPress에서 this.forgotPasswordForm()을 호출했기 때문에 그 오류가 발생했습니다. 단지 onPress = { this.props.forgotPasswordForm} ' –