[英]Pass event handler as props to react-native custom component
I have a custom component called MyButton.js 我有一个名为MyButton.js的自定义组件
<View style={{ alignItems: 'center', width: '100%' }}>
<TouchableOpacity
style={[styles.button, props.extraStyle]}
onPress={props.onPress}
>
<Text style={styles.buttonText}>{props.children}</Text>
</TouchableOpacity>
</View>
);
I pass the onPress handler for the button via props from parent SignUp.js 我通过父SignUp.js的道具传递按钮的onPress处理程序
<MyButton
extraStyle={{ backgroundColor: '#03A9F4' }}
onPress={this.signUpUser}
>
SIGN UP
</MyButton>
Problem is that my signUpUser function (also in SignUp.js) needs to be passed 3 arguments 问题是我的signUpUser函数(也在SignUp.js中)需要传递3个参数
signUpUser = (userName, email, password) => {
try {
//handle password<6 and bad email format etc pls!!!!
Firebase.auth()
.createUserWithEmailAndPassword(email, password)
.then((user) => {
console.log('User created in firebase', user);
//update FB with userNameNode......and default values for profile
Firebase.database()
.ref('profiles/users/' + user.user.uid)
.set({
active: false,
emailAddress: email,
userName: userName,
userId: user.user.uid,
................
................
If I change the onPress prop of MyButton compoment as below to pass args....I get error in that it logs user in before theyve even pressed the button 如果我如下更改MyButton compoment的onPress属性以传递args ....我得到的错误是,它在用户按下按钮之前就将用户登录
<MyButton
extraStyle={{ backgroundColor: '#03A9F4' }}
onPress={this.signUpUser(
this.state.userName,
this.state.email,
this.state.password,
)}
>
SIGN UP
</MyButton>
Can someone please help? 有人可以帮忙吗? Ive been up all night with this lol
我已经整夜与这个哈哈
try to change 尝试改变
onPress={this.signUpUser(blabla)}
onPress = {this.signUpUser(blabla)}
to 至
onPress={() => this.signUpUser(blabla)}
onPress = {()=> this.signUpUser(blabla)}
There is a similar post here, React Native onPress being called automatically 这里有一个类似的帖子, React Native onPress被自动调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.