0

I am trying to figure out how to define a link to reference that can use a firebase document id to link to a show view for that document. I can render an index. I cannot find a way to define a link to the document.

I've followed this tutorial - which is good to get the CRUD steps other than the show view. I can find other tutorials that do this with class components and the closest I've been able to find using hooks is this incomplete project repo.

I want to try and add a link in the index to show the document in a new view.

I have an index with:

  const useBlogs = () => {
const [blogs, setBlogs] = useState([]); //useState() hook, sets initial state to an empty array
  useEffect(() => {
    const unsubscribe = Firebase
      .firestore //access firestore
      .collection("blog") //access "blogs" collection
      .where("status", "==", true)
      .orderBy("createdAt")
      .get()
      .then(function(querySnapshot) {
    //   .onSnapshot(snapshot => {
        //You can "listen" to a document with the onSnapshot() method.
        const listBlogs =  querySnapshot.docs.map(doc => ({
          //map each document into snapshot
          id: doc.id, //id and data pushed into blogs array
          ...doc.data() //spread operator merges data to id.
        }));
        setBlogs(listBlogs); //blogs is equal to listBlogs
      });
    return 
    // () => unsubscribe();
  }, []);
  return blogs;
};

const BlogList = ({ editBlog }) => {
  const listBlog = useBlogs();
  return (
    <div>
      {listBlog.map(blog => (
        <Card key={blog.id} hoverable={true} style={{marginTop: "20px", marginBottom: "20px"}}>

            <Title level={4} >{blog.title} </Title>
            <Tag color="geekblue" style={{ float: "right"}}>{blog.category} </Tag>
            <Paragraph><Text>{blog.caption}

            </Text></Paragraph> 
            <Link to={`/readblog/${blog.id}`}>Read</Link>
            <Link to={`/blog/${blog.id}`}>Read</Link>
        </Card>       
      ))}
      </div>
  );
};
export default BlogList;

Then I have a route defined with:

export const BLOGINDEX = '/blog';
export const BLOGPOST = '/blog/:id';
export const NEWBLOG = '/newblog';
export const EDITBLOG = '/editblog';
export const VIEWBLOG = '/viewblog';
export const READBLOG = '/readblog/:id';    

I can't find a tutorial that does this with hooks. Can anyone see how to link from an index to a document that I can show in a different page?

I did find this code sandbox. It looks like it is rendering a clean page in the updateCustomer page and using data from the index to do it - but the example is too clever for me to unpick without an explanation of what's happening (in particular, the updateCustomer file defines a setCustomer variable, by reference to useForm - but there is nothing in useForm with that definition. That variable is used in the key part of the file that tries to identify the data) - so I can't mimic the steps.

NEXT ATTEMPT

I found this blog post which suggests some changes for locating the relevant document.

I implemented these changes and while I can print the correct document.id on the read page, I cannot find a way to access the document properties (eg: blog.title).

import React, { useHook } from 'react';
import {
    useParams
  } from 'react-router-dom';

import Firebase from "../../../firebase";
import BlogList from './View';


function ReadBlogPost() {
    let { slug } = useParams()
        // ...


    return (
        <div>{slug} 
        </div>
    )
};

export default ReadBlogPost;

NEXT ATTEMPT:

I tried to use the slug as the doc.id to get the post document as follows:

import React, { useHook, useEffect } from 'react';
import {
    useParams
  } from 'react-router-dom';



import Firebase from "../../../firebase";
import BlogList from './View';


function ReadBlogPost() {
    let { slug } = useParams()
        // ...
    useEffect(() => {
        const blog = 
    Firebase.firestore.collection("blog").doc(slug);

            blog.get().then(function(doc) {
                if (doc.exists) {
                    console.log("Document data:", doc.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);
            });
        });


        return (
            <div>{blog.title} 
            </div>
        )
    };

    export default ReadBlogPost;

It returns an error saying blog is not defined. I also tried to return {doc.title} but I get the same error. I can see all the data in the console.

I really can't make sense of coding documentation - I can't figure out the starting point to decipher the instructions so most things I learn are by trial and error but I've run out of places to look for inspiration to try something new.

NEXT ATTEMPT

My next attempt is to try and follow the lead in this tutorial.

function ReadBlogPost(blog) {
    let { slug } = useParams()
        // ...
    useEffect(() => {
        const blog = 
Firebase.firestore.collection("blog").doc(slug);

        blog.get().then(function(doc) {
            if (doc.exists) {
                doc.data()
                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);
        });

    },
    [blog]
);


    return (
        <div><Title level={4} > {blog.title}

        </Title>

        <p>{console.log(blog)}</p>


        </div>
    )
};

export default ReadBlogPost;

When I try this, the only odd thing is that the console.log inside the useEffect method gives all the data accurately, but when I log it form inside the return method, I get a load of gibberish (shown in the picture below).enter image description here

NEXT ATTEMPT

I found this tutorial, which uses realtime database instead of firestore, but I tried to copy the logic.

My read post page now has:

import React, { useHook, useEffect, useState } from 'react';
import {
    useParams
  } from 'react-router-dom';

import Firebase from "../../../firebase";
import BlogList from './View';
import { Card, Divider, Form, Icon, Input, Switch, Layout, Tabs, Typography, Tag, Button } from 'antd';
const { Paragraph, Text, Title } = Typography;


 const ReadBlogPost = () => {
    const [loading, setLoading] = useState(true);
    const [currentPost, setCurrentPost] = useState();



    let { slug } = useParams()

    if (loading && !currentPost) {
        Firebase
          .firestore
          .collection("blog")
          .doc(slug)
          .get()
          .then(function(doc) {
            if (doc.exists) {
                setCurrentPost(...doc.data());
                console.log("Document data:", doc.data());
            }
          }),  

          setLoading(false)
          }

          if (loading) {
            return <h1>Loading...</h1>;
          }





    return (
        <div><Title level={4} > 
        {currentPost.caption}
        {console.log({currentPost})}

        </Title>




        </div>
    )

};
export default ReadBlogPost;

Maybe this blog post is old, or maybe it's to do with it using .js where I have .jsx - which I think means I can't use if statements, but I can't get this to work either. The error says:

Line 21:9: Expected an assignment or function call and instead saw an expression no-unused-expressions

It points to the line starting with Firebase.

I got rid of all the loading bits to try and make the data render. That gets rid of the above error message for now. However, I still can't return the values from currentPost.

It's really odd to me that inside the return statement, I cannot output {currentPost.title} - I get an error saying title is undefined, but when I try to output {currentPost} the error message says:

Error: Objects are not valid as a React child (found: object with keys {caption, category, createdAt, post, status, title}). If you meant to render a collection of children, use an array instead.

That makes no sense! I'd love to understand why I can log these values before the return statement, and inside the return statement, I can log them on the object but I cannot find how to log them as attributes.

Mel
  • 2,481
  • 26
  • 113
  • 273

2 Answers2

0

First of all: is your useBlog() hook returning the expected data? If so, all you need to do is define your <Link/> components correctly.

<Link 
  // This will look like /readblog/3. Curly braces mean
  // that this prop contains javascript that needs to be
  // evaluated, thus allowing you to create dynamic urls.
  to={`/readblog/${blog.id}`}
  // Make sure to open in a new window
  target="_blank"
>
  Read
</Link>

Edit: If you want to pass the data to the new component you need to set up a store in order to avoid fetching the same resource twice (once when mounting the list and once when mounting the BlogPost itself)

// Define a context
const BlogListContext = React.createContext()

// In a top level component (eg. App.js) define a provider
const App = () => {
  const [blogList, setBlogList] = useState([])

  return (
    <BlogListContext.Provider value={{blogList, setBlogList}}>
      <SomeOtherComponent/>
    </BlogListContext.Provider>
  )
}

// In your BlogList component
const BlogList = ({ editBlog }) => {
  const { setBlogList } = useContext(BlogListContext)
  const listBlog = useBlogs()

  // Update the blog list from the context each time the
  // listBlog changes
  useEffect(() => {
    setBlogList(listBlog)
  }, [listBlog])
  return (
    // your components and links here
  )
}

// In your ReadBlog component
const ReadBlogComponent = ({ match }) => {
  const { blogList } = useContext(BlogListContext)
  // Find the blog by the id from params.
  const blog = blogList.find(blog => blog.id === match.params.id) || {}

  return (
    // Your JSX
  )
}

There are other options for passing data as well:

  1. Through url params (not recommended).
  2. Just pass the ID and let the component fetch its own data on mount.
Giorgio Zanni
  • 584
  • 5
  • 10
  • It works to render a page with a url that has the right doc id in it, but I can't find a way to then use the document data in the readblog page to display the data. I don't know how to be able to do: blog.title in that page etc. – Mel Apr 10 '20 at 23:03
  • Oh, it wasn't clear to me what was your problem. I'll edit the answer. – Giorgio Zanni Apr 10 '20 at 23:17
  • I've updated the answer, please let me know if this works for you. – Giorgio Zanni Apr 11 '20 at 00:00
  • Thanks - I'll try to work through this. Do you have any suggestions for where I could learn more about the 2nd of your alternative options? – Mel Apr 11 '20 at 00:18
  • You are already passing the blog id as a url param (React Navigation does this for you). All you need to do is to get the ID from the `match.params` object (docs here https://reacttraining.com/react-router/web/api/match) and then use an useEffect hook to call the firebase api on mount passing the ID. – Giorgio Zanni Apr 11 '20 at 00:30
  • This is what I am trying to figure out how to do. I've just tried the context solution you provided above - I must be missing something because I can't get it to recognise the provider. Maybe I can try this match.params solution to work through this problem instead. Do you know where I can find an example of the useEffect solution? – Mel Apr 11 '20 at 00:33
  • I found this blog: https://reacttraining.com/blog/react-router-v5-1/. I'll try to follow it's ideas. Thanks for your help – Mel Apr 11 '20 at 00:46
0

I found an answer that works for each attribute other than the timestamp.

const [currentPost, setCurrentPost] = useState([]);

There is an empty array in the useState() initialised state.

In relation to the timestamps - I've been through this hell so many times with firestore timestamps - most recently here. The solution that worked in December 2019 no longer works. Back to tearing my hair out over that one...

Mel
  • 2,481
  • 26
  • 113
  • 273