0

I am doing my first application using react native and firebase so I know how to configure like this

var config = {
  apiKey: "<API_KEY>",
  authDomain: "<PROJECT_ID>.firebaseapp.com",
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
  storageBucket: "<BUCKET>.appspot.com",
};
firebase.initializeApp(config);

But I don't know how to list add or update data somebody can help me I've tried this

firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
});
1
  • What's the error?
    – Eric Guan
    Commented Jun 27, 2017 at 15:59

1 Answer 1

1

There is a good step by step ReactFire tutorial on Firebase official website. Check here https://www.firebase.com/docs/web/libraries/react/guide.html

Step 1: Add firebase to you project with in head tag.

<!-- Inside head -->
<script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>

Step 2: Create reference to users node (or whatever node you have in your database) You can do it with in componentWillMount() method. (assume your firebase node is user)

componentWillMount() {
    this.firebaseRef = new Firebase("https://YourApp.firebaseio.com/users");
}

Step 3: To add data into firebase (Assume form calls handleSubmit function when it gets submitted)

handleSubmit: function(e) {
   e.preventDefault();
   this.firebaseRef.push({
      username: 'username',
      email: '[email protected]',
      imageUrl: 'some image url'
   });
   this.setState({text: ""});
}

Step 4: Clean up database handler when your component unmounts.

componentWillUnmount: function() {
   this.firebaseRef.off();
}
1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.