简体   繁体   English

如何在React Native中使用reactfire

[英]How to use reactfire with react native

I'm trying to use 'reactfire' mixin with react native for my app. 我正在尝试将“ reactfire” mixin与我的应用程序的本机反应一起使用。

I installed the npm module. 我安装了npm模块。

$ npm install reactfire --save

I included the module in my 'index.ios.js' file 我将模块包含在“ index.ios.js”文件中

var ReactFireMixin = require('./node_modules/reactfire');

I added the mixin to the class 我将mixin添加到了课堂上

mixins: [ReactFireMixin],

I say 我说

getInitialState: function() {
  return {
    items: [],
  }
},

componentWillMount: function() {
  var ref = new Firebase('https://<MY APP NAME>.firebaseio.com/tasks');
  this.bindAsArray(ref, 'items');
  console.log(ref);
  console.log(this.state.items);
}

And I get 我得到

调试器输出

I can use the 'firebase' npm module just fine but not the 'reactfire' one, and I'd like to us the mixin in my app instead of the other alternatives. 我可以很好地使用“ firebase” npm模块,但不能使用“ reactfire”模块,我想在我们的应用程序中使用mixin代替其他替代方案。

I'm also curious about how this particular mixin handles offline usage. 我也对这个特殊的混合如何处理离线使用感到好奇。

PS: flaw in firebase documentation because just doing 'mixins: [ReactFireMixin],' doesn't do anything and when developing for react native you don't have a html file. PS:firebase 文档中的缺陷,因为仅执行“ mixins:[ReactFireMixin]”就不会做任何事情,并且在开发本机React时,您没有html文件。

And yes, I'm a react newb, so this might just be an utter waste of your time. 是的,我是新手,所以这可能只是浪费您的时间。

Here's the full code - github repo 这是完整的代码-github repo

I just tested and don't see any problems when I use ReactFire in an React Native app (for Android at least). 我刚刚进行了测试,但在React Native应用程序(至少适用于Android)中使用ReactFire时没有看到任何问题。

My entire index.android.js : 我整个index.android.js

'use strict';

var React = require('react-native');
var Firebase = require('firebase');
var ReactFireMixin = require('reactfire');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TextInput,
  TouchableHighlight,
} = React;

var CoolProject = React.createClass({
  mixins: [ReactFireMixin],
  getInitialState: function() {
    return {
      items: []
    };
  },
  componentWillMount: function() {
    this.ref = new Firebase('https://nanochat.firebaseio.com/chat');
    this.bindAsArray(this.ref, 'items');
  },
  _onPressButton: function() {
    this.ref.push({ name: 'puf', message: this.state.text });
  },
  render: function() {
    var createItem = function(item, index) {
      return <Text>{item.name}: {item.message}</Text>
    };
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        {this.state.items.map(createItem)}
        <View style={styles.horizontal}>
          <TextInput onChangeText={(text) => 
              this.setState({ text: text})} value={this.state.text} />
          <TouchableHighlight onPress={this._onPressButton}>
            <Text>Send</Text>
          </TouchableHighlight>
        </View>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('CoolProject', () => CoolProject);

Note that in many cases it is not necessary to use ReactFire. 请注意,在许多情况下,没有必要使用ReactFire。 For example, this is the code that I was using instead of this.bindAsArray(ref, 'items') : 例如,这是我使用的代码,而不是this.bindAsArray(ref, 'items')

So if ReactFire is causing you problems, consider getting the same working without ReactFire. 因此,如果ReactFire引起您的问题,请考虑在没有ReactFire的情况下也能正常工作。

this.ref.on('value', function(snapshot) {
  var items = [];
  snapshot.forEach(function(child) {
    items.push(child.val());
  });
  this.setState({ 'items': items });
}.bind(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM