Javascript 为什么它在挂载时写入firebase,而不是在单击时写入?
嗨,我有一个函数,当点击调用它时,它将写入firebase,尝试了几个小时,但没有成功。我想写到FB点击,而不是挂载这里是我的代码 Home.jsJavascript 为什么它在挂载时写入firebase,而不是在单击时写入?,javascript,firebase,react-native,firebase-realtime-database,Javascript,Firebase,React Native,Firebase Realtime Database,嗨,我有一个函数,当点击调用它时,它将写入firebase,尝试了几个小时,但没有成功。我想写到FB点击,而不是挂载这里是我的代码 Home.js render() { return ( <SideMenu menu={menu} isOpen={this.state.isOpen} onChange={isOpen => this.updateMenuState(isOpen)} >
render() {
return (
<SideMenu
menu={menu}
isOpen={this.state.isOpen}
onChange={isOpen => this.updateMenuState(isOpen)}
>
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Button onPress={this.storeHighScore('9', 'Hello world')} // < HERE
buttonStyle={{ backgroundColor: 'rgb(30, 144, 255)', borderRadius: 10 }}
textStyle={{ textAlign: 'center' }}
title="Add" />
</View>
</SideMenu>
);
}
//it should call this function on click
storeHighScore(num, value) {
firebase.database().ref('notes/'+num).set({txt :value});
};
}
提前感谢您应该这样使用
<Button onPress={() => this.storeHighScore('9', 'Hello world')}
this.storeHighScore('9','helloworld'))
这样,onPress将等待其内部的箭头函数被调用(通过您的按下操作),然后触发storeHighScore。
如果不实例化该函数,则在其呈现后将调用storeHighScore
希望对您有所帮助您应该这样使用
<Button onPress={() => this.storeHighScore('9', 'Hello world')}
this.storeHighScore('9','helloworld'))
这样,onPress将等待其内部的箭头函数被调用(通过您的按下操作),然后触发storeHighScore。
如果不实例化该函数,则在其呈现后将调用storeHighScore
希望能有帮助