为什么它写入mount上的firebase而不是点击它反应原生?

时间:2017-09-06 20:48:19

标签: javascript firebase react-native firebase-realtime-database

嗨我有一个函数,当点击它时,它将写入firebase,尝试几个小时但没有成功。我想在点击时写入FB,而不是在mount上这是我的代码。

Home.js

<DefaultItemExcludes>$(DefaultItemExcludes);ClientLib\node_modules\**</DefaultItemExcludes>

我在登录页面中调用firebase配置,这在主页之前调用

Login.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});
      };
     }

提前致谢

1 个答案:

答案 0 :(得分:3)

你应该这样使用

<Button onPress={() => this.storeHighScore('9', 'Hello world')}

这样onPress会等待它内部的箭头功能(通过你的按下动作)然后触发storeHighScore。 如果您没有对该函数进行实例化,那么storeHighScore将在渲染后立即调用。

希望有所帮助