admin管理员组文章数量:1414604
I am fairly new to React and Firebase/Firestore. I have gone through the Firestore docs a few times and still feel lost. Here's what I know how to do... I know how to add to my Firestore DB, and I know how to print data to the console. But I don't know how to render that data? Can anyone please guide me on how to do this? Thanks
import React, { Component } from 'react';
import './App.css';
import { database } from './firebase/firebase';
class App extends Component {
constructor(props) {
super(props);
this.state = {
inputField: ''
};
}
onInputChange = (e) => {
const inputField = e.target.value;
this.setState(() => ({ inputField }));
}
/* Adding Data */
onSubmit = (e) => {
e.preventDefault();
console.log(this.state.inputField);
database.collection('users').add({
name: this.state.inputField
})
.then((docRef) => {
console.log("Doc written with ID: ", docRef.id)
})
.catch((error) => {
console.error("Error adding document: ", error);
});
}
/* Retrieving Data */
onLoad = (e) => {
const docRef = database.collection('users').doc('O6m4TFfIjE42ZaNfvC7s');
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
}
render() {
return (
<div className="App">
<h1>Raul: <p>{}</p></h1>
<form onSubmit={this.onSubmit}>
<input
type="text"
value={this.state.inputField}
onChange={this.onInputChange}
/>
<button>Save</button>
</form>
<button onClick={this.onLoad}>Load Data</button>
</div>
);
}
}
export default App;
I am fairly new to React and Firebase/Firestore. I have gone through the Firestore docs a few times and still feel lost. Here's what I know how to do... I know how to add to my Firestore DB, and I know how to print data to the console. But I don't know how to render that data? Can anyone please guide me on how to do this? Thanks
import React, { Component } from 'react';
import './App.css';
import { database } from './firebase/firebase';
class App extends Component {
constructor(props) {
super(props);
this.state = {
inputField: ''
};
}
onInputChange = (e) => {
const inputField = e.target.value;
this.setState(() => ({ inputField }));
}
/* Adding Data */
onSubmit = (e) => {
e.preventDefault();
console.log(this.state.inputField);
database.collection('users').add({
name: this.state.inputField
})
.then((docRef) => {
console.log("Doc written with ID: ", docRef.id)
})
.catch((error) => {
console.error("Error adding document: ", error);
});
}
/* Retrieving Data */
onLoad = (e) => {
const docRef = database.collection('users').doc('O6m4TFfIjE42ZaNfvC7s');
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
}
render() {
return (
<div className="App">
<h1>Raul: <p>{}</p></h1>
<form onSubmit={this.onSubmit}>
<input
type="text"
value={this.state.inputField}
onChange={this.onInputChange}
/>
<button>Save</button>
</form>
<button onClick={this.onLoad}>Load Data</button>
</div>
);
}
}
export default App;
Share
Improve this question
edited Jan 27, 2018 at 19:39
Raul Sanchez
asked Jan 27, 2018 at 19:30
Raul SanchezRaul Sanchez
2,0293 gold badges14 silver badges7 bronze badges
2 Answers
Reset to default 3you can save the data in the state
constructor(props)
{
super(props);
this.state = {
inputField: '',
data: null,
};
}
save the data to state when data loaded
onLoad = (e) => {
const docRef = database.collection('users').doc('O6m4TFfIjE42ZaNfvC7s');
docRef.get().then((doc) => {
if (doc.exists) {
let data = doc.data();
this.setState({ data: data });
console.log("Document data:", data);
} else {
// doc.data() will be undefined in this case
this.setState({ data: null });
console.log("No such document!");
}
}).catch(function (error) {
this.setState({ data: null });
console.log("Error getting document:", error);
});
}
and then render them as you want, I will display them as json in <pre>
tag
render() {
let dataUI = this.state.data ? <h1>No Data</h1> : <pre>{JSON.stringify(this.state.data)}</pre>;
return (
<div className="App">
<h1>Raul: </h1>
<div>
<h1>UI Data</h1>
{dataUI}
</div>
<form onSubmit={this.onSubmit}>
<input
type="text"
value={this.state.inputField}
onChange={this.onInputChange}
/>
<button>Save</button>
</form>
<button onClick={this.onLoad}>Load Data</button>
</div>
);
}
Using the Firebase/Firestore API directly in React can be quite cumbersome and lead to a lot of boilerplate. You could use Firestorter, which uses mobx behind the scenes to hook up your ponents to Firestore within seconds.
https://github./IjzerenHein/firestorter
本文标签: javascriptHow do I render Firestore data in ReactStack Overflow
版权声明:本文标题:javascript - How do I render Firestore data in React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745193318a2647017.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论