Salutare,
Încerc să implementez sign-up cu e-mail. Am un ecran ce conține un TextInput
; vreau să folosesc acest ecran în mai multe părți din aplicație. Acest TextInputScreen
conține o componentă TextInputComponent
. Aici, utilizatorul introduce e-mailul său. Dacă e-mailul este invalid, arunc o eroare și încerc să actualizez mesajul de eroare din ecranul meu TextInputScreen
.
Problema cu care mă confrunt acum este că mesajul de eroare de la TextInputComponent
nu este actualizat atunci când este aruncată o eroare.
Fluxul este acesta:
- Utilizatorul apasă pe „Înregistrare cu e-mail” într-un ecran separat -> openEmailScreen este apelat
- Utilizatorul introduce un e-mail și apasă pe tastatură butonul „Done” ->
inputReceived
este apelat - Dacă e-mailul este invalid -> o eroare este aruncată în
inputReceived
și mesajul de eroare este afișat înTextInputViewComponent
Actualizarea mesajului de eroare la pasul 3 NU funcționează momentan și nu îmi dau seama cum să o fac să funcționeze.
Acesta este EmailConnector
:
export default class EmailConnector {
static keyboardTypes = {
email: 'email-address',
default: 'default',
};
static openEmailScreen = async navigation => {
navigation.navigate('TextInputScreen', {
placeholder: strings.onboarding.email_flow.email_placeholder,
keyboardType: this.keyboardTypes.email,
onKeyboardPressed: () => this.inputReceived(),
errorMessage: 'placeholder message',
})
}
//method called when the "Done" button from the keyboard is pressed
static inputReceived = () => {
try {
const email = new SignUpUserBuilder().setEmail('testexample.com').build();//used to validate the email
}
catch(error) {
console.log(error);
****//HERE I need to figure out a way to change props.errorMessage and force TextInputViewComponent to rerender****
<TextInputViewComponent errorMessage = 'Invalid email'/>;
const viewComponent = new TextInputViewComponent();
viewComponent.forceUpdate();
}
}
}
Acesta este TextInputScreen
:
class TextInputScreen extends Component {
render() {
return (
<View style={styles.rootView}>
<TextInputViewComponent
placeholder={this.props.route.params.placeholder}
keyboardType={this.props.route.params.keyboardType}
onKeyboardPressed={this.props.route.params.onKeyboardPressed}
errorMessage={this.props.route.params.errorMessage}
/>
</View>
)
}
}
export default TextInputScreen;
Și acesta este TextInputViewComponent
:
class TextInputViewComponent extends Component {
constructor(props) {
super(props);
this.state = {
shouldRefreshComponent: false
}
}
refreshComponent = () => {
this.setState({
shouldRefreshComponent: true
})
}
render() {
return (
<View>
<TextInput
placeholder={this.props.placeholder}
placeholderTextColor={colors.placeholder}
keyboardType={this.props.keyboardType}
style={styles.textInput}
onSubmitEditing= {() => this.props.onKeyboardPressed()}
/>
<Text
style={{fontSize: 18, color: colors.white}}
ref={Text => { this._textError = Text}}>
{this.props.errorMessage}
</Text>
</View>
)
}
}
export default TextInputViewComponent;
Din metoda inputReceived
, am încercat să apelez forceUpdate
și setState
pentru TextInputViewComponent, dar în ambele cazuri primesc mesajul: "Can't call forceUpdate/setState on a component that is not yet mounted"
Cum pot actualiza mesajul de eroare din TextInputViewComponent
? Mulțumesc!