[英]React Native change two parents state in a single function
I'm trying to change two states in the parent component of my loginForm
with in one EventListner (if this is the right term for the function). 我试图在一个EventListner中更改
loginForm
的父组件中的两个状态(如果这是该函数的正确术语)。
Currently I'm only able to pass the value of one event trough to the parent, but I want to do both (for efficient coding) 目前我只能将一个事件的值传递给父级,但我想两者都做(为了有效编码)
The only solution I've found so far is to create two functions, which do essentially the same. 到目前为止,我发现的唯一解决方案是创建两个基本相同的函数。 (change a state) So far I've been told it's bad ethics to create the same function twice
(改变一个州)到目前为止,我被告知两次创建相同的功能是不好的道德
Parent Component 父组件
changeStateValue = setEmail => {
this.setState({
email:setEmail,
password: setPassword,
email_valid: this.validateEmail(setEmail),
})
}
render() {
const { email, password, email_valid, showLoading, } = this.state;
return (
<View style={styles.container}>
<ImageBackground source={BG_IMAGE} style={styles.bgImage}>
{this.state.fontLoaded ?
(
<LoginForm
loginButton={this.loginButton}
setEmail={email}
setPassword={password}
password={password}
email_valid={email_valid}
showLoading={showLoading}
navigation={this.props.navigation}
changeStateValue={this.changeStateValue}
/>
)
Child Component 子组件
export default function LoginForm (props) {
const { email, password, email_valid, showLoading } = props;
return(
<View style={styles.loginView}>
<View style={styles.loginTitle}>
<Image
style={styles.imageCard}
source={RAPIO_LOGO}
/>
</View>
<View style={styles.loginInput}>
<FormInput
icon="user"
containerStyle={{ marginVertical: 10 }}
onChangeText={setEmail => {
props.changeStateValue(setEmail)
// this.passwordInput.focus();
}}
value={email}
inputStyle={{ marginLeft: 10, color: 'white' }}
keyboardAppearance="light"
placeholder="Email"
autoFocus={false}
autoCapitalize="none"
autoCorrect={false}
keyboardType="email-address"
returnKeyType="next"
onSubmitEditing={() => {
props.changeStateValue(e)
this.passwordInput.focus();
}}
blurOnSubmit={false}
placeholderTextColor="white"
errorStyle={{ textAlign: 'center', fontSize: 12 }}
errorMessage={
email_valid ? null : 'Please enter a valid email address'
}
/>
<FormInput
icon="lock"
containerStyle={{ marginVertical: 10 }}
onChangeText={setPassword => {
props.changeStateValue(setPassword)
// this.passwordInput.focus();
}}
value={password}
inputStyle={{ marginLeft: 10, color: 'white' }}
secureTextEntry={true}
keyboardAppearance="light"
placeholder="Password"
autoCapitalize="none"
autoCorrect={false}
keyboardType="default"
returnKeyType="done"
blurOnSubmit={true}
placeholderTextColor="white"
onSubmitEditing={props.loginButton}
/>
</View>
<Button
title="LOG IN"
containerStyle={{ flex: -1 }}
buttonStyle={styles.signUpButton}
titleStyle={styles.signUpButtonText}
onPress={props.loginButton}
loading={showLoading}
loadingProps={{ size: 'small', color: 'white' }}
disabled={!email_valid && password.length < 8}
/>
<View style={styles.footerView}>
<Text style={{ color: 'white', marginBottom: 15, }}>New here?</Text>
<Button
title="Create an Account"
clear
activeOpacity={0.5}
titleStyle={styles.loginHereText}
titleStyle={{ color: '#dd016b', fontSize: 15 }}
buttonStyle={{ backgroundColor: 'transparent' }}
containerStyle={{ marginTop: -10 }}
onPress={() => this.props.navigation.navigate('SignupView')}
/>
</View>
</View>
)
}
I fixed it. 我修好了它。 Sorry for the confusing title.
抱歉这个令人困惑的标题。 It wasn't using a event listener, but a regular event.
它没有使用事件监听器,而是常规事件。
What I needed to do was create a normal function where I pass on two parameters, and when I pass on the parameters in my child I needed to identify both parameters 我需要做的是创建一个普通函数,我传递两个参数,当我传递我孩子的参数时,我需要识别这两个参数
Function in the parent 父母的功能
changeStateValue =(setEmail, setPassword)=> {
this.setState({
email:setEmail,
password: setPassword,
email_valid: this.validateEmail(setEmail),
})
}
Passing on parameters in child 传递孩子的参数
//For email input
onChangeText={setEmail => {
props.changeStateValue(setEmail, props.setPassword)
}}
//For password input
onChangeText={setPassword => {
props.changeStateValue(setPassword, props.setEmail)
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.