Create Data with Firestore Create a new post document in Firestore with a custom slug ID

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
SignUp for Unlimited PRO Access


*Enrollment provides full access to this course (and updates) for life.

Install Lodash kebabCase

command line
npm i lodash.kebabcase

Firebase Server Timestamp

The server timestamp ensures data time-based data will be consistent for all users.

file_type_js_official lib/firebase.js
export const serverTimestamp = firebase.firestore.FieldValue.serverTimestamp;

Create a Post

The post form will set default values for in Firestore. It will also generate a URI safe slug as the Firebase document ID.

file_type_js_official posts/admin/index.js
import styles from '../../styles/Admin.module.css';
import AuthCheck from '../../components/AuthCheck';
import PostFeed from '../../components/PostFeed';
import { UserContext } from '../../lib/context';
import { firestore, auth, serverTimestamp } from '../../lib/firebase';

import { useContext, useState } from 'react';
import { useRouter } from 'next/router';

import { useCollection } from 'react-firebase-hooks/firestore';
import kebabCase from 'lodash.kebabcase';
import toast from 'react-hot-toast';

export default function AdminPostsPage(props) {
  return (
        <PostList />
        <CreateNewPost />

function PostList() {
  const ref = firestore.collection('users').doc(auth.currentUser.uid).collection('posts');
  const query = ref.orderBy('createdAt');
  const [querySnapshot] = useCollection(query);

  const posts = querySnapshot? =>;

  return (
      <h1>Manage your Posts</h1>
      <PostFeed posts={posts} admin />

function CreateNewPost() {
  const router = useRouter();
  const { username } = useContext(UserContext);
  const [title, setTitle] = useState('');

  // Ensure slug is URL safe
  const slug = encodeURI(kebabCase(title));

  // Validate length
  const isValid = title.length > 3 && title.length < 100;

  // Create a new post in firestore
  const createPost = async (e) => {
    const uid = auth.currentUser.uid;
    const ref = firestore.collection('users').doc(uid).collection('posts').doc(slug);

    // Tip: give all fields a default value here
    const data = {
      published: false,
      content: '# hello world!',
      createdAt: serverTimestamp(),
      updatedAt: serverTimestamp(),
      heartCount: 0,

    await ref.set(data);

    toast.success('Post created!')

    // Imperative navigation after doc is set


  return (
    <form onSubmit={createPost}>
        onChange={(e) => setTitle(}
        placeholder="My Awesome Article!"
        <strong>Slug:</strong> {slug}
      <button type="submit" disabled={!isValid} className="btn-green">
        Create New Post

Q&A Chat