Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么它在挂载时写入firebase,而不是在单击时写入?_Javascript_Firebase_React Native_Firebase Realtime Database - Fatal编程技术网

Javascript 为什么它在挂载时写入firebase,而不是在单击时写入?

Javascript 为什么它在挂载时写入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)} >

嗨,我有一个函数,当点击调用它时,它将写入firebase,尝试了几个小时,但没有成功。我想写到FB点击,而不是挂载这里是我的代码

Home.js

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

希望能有帮助