Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

reactjs - How to fetch and display data from documents in subcollection in react

I want to display data from firestore in the simplest way because I'm a beginner in react. Here is the tree of my data of firestore.

collection-[ documentsList [ subcolletcion [ documentsList [ data

question from:https://stackoverflow.com/questions/65870747/how-to-fetch-and-display-data-from-documents-in-subcollection-in-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To fetch data you can do it like this:

import React, { useState, useEffect } from 'react';
import axios from 'axios';

function App() {
  const [data, setData] = useState(null);

  useEffect(() => {
     const getData = async () => {         
       const result = await axios('https://route_to_backend.com/bla-bla'); 
       setData(result.data);
     }
     getData();
  }, []);

  return (
    <ul>
      {data && data.map(item => (
        <li key={item.id}>
          <div>{item.title}</div>
        </li>
      ))}
    </ul>
  );
}

export default App;

If you provide some example of objects that you receive from server I can write code how you can render it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...