Describe the bug
I'm attempting to build a Gatsby Netlify CMS using https://github.com/netlify-templates/gatsby-starter-netlify-cms/ as a guideline.
My project is very similar in most ways. When I attempt to add a preview pane using the following structure:
import CMS from 'netlify-cms'
import BlogPostPreview from './preview-templates/BlogPostPreview'
CMS.registerPreviewTemplate('blog', BlogPostPreview)
I get the following error: Objects are not valid as a React child
My BlogPostPreview looks like this:
import React from 'react'
import BlogPostTemplate from '../../components/BlogPostTemplate'
const BlogPostPreview = ({ entry, widgetFor }) => (
<BlogPostTemplate
date={entry.getIn(['data', 'date'])}
title={entry.getIn(['data', 'title'])}
html={widgetFor('body')}
/>
)
export default BlogPostPreview
To Reproduce
Steps to reproduce the behavior:
(CMS.registerPreviewTemplate('blog', BlogPostPreview)Objects are not valid as a React childExpected behavior
DIsplay a styled preview of the component
Applicable Versions:
CMS configuration
backend:
name: github
repo: jonathanphz/gatsby-netlify-cms-exploration
media_folder: static/assets
public_folder: assets
collections:
- name: blog
label: Blog
folder: blog
create: true
fields:
- { label: Template Key, name: templateKey, widget: hidden, default: blog-post }
- { name: path, label: Path }
- { name: date, label: Date, widget: date }
- { name: tumbnail, label: Featured Image, widget: image }
- { name: title, label: Title }
- { name: body, label: Body, widget: markdown }
- name: pages
label: Pages
files:
- file: src/pages/home/index.md
label: Home
name: home
fields:
- { name: path, label: Path }
- { label: Template Key, name: templateKey, widget: hidden, default: home-page }
- { label: Title, name: title, widget: string }
- { label: Hero, name: hero, widget: object, fields: [{label: Heading, name: heading, widget: string}, {label: Description, name: description, widget: text}]}
- { label: Image and Text Two Column, name: imageTextTwoColumn, widget: object, fields:
[
{label: Reverse Layout, name: reverseLayout, widget: boolean, default: false },
{label: Image, name: image, widget: object, fields: [{label: Image Upload, name: imagePath, widget: image}, {label: Image Alt Text, name: imageAlt, widget: string}]},
{label: Text, name: text, widget: markdown }
]
}
- file: src/pages/about/index.md
label: About
name: about
fields:
- { name: path, label: Path }
- { label: Template Key, name: templateKey, widget: hidden, default: home-page }
- { label: Title, name: title, widget: string }
- { label: Hero, name: hero, widget: object, fields: [{label: Heading, name: heading, widget: string}, {label: Description, name: description, widget: text}]}
- { label: Image and Text Two Column, name: imageTextTwoColumn, widget: object, fields:
[
{label: Reverse Layout, name: reverseLayout, widget: boolean, default: false },
{label: Image, name: image, widget: object, fields: [{label: Image Upload, name: imagePath, widget: image}, {label: Image Alt Text, name: imageAlt, widget: string}]},
{label: Text, name: text, widget: markdown }
]
}
Additional context
Add any other context about the problem here.
That generally means something you're outputting in the component is an object. If your repo is public can you link it?
Hi,
Yeah here it is: https://github.com/jonathanphz/gatsby-netlify-cms-exploration
@jonathanphz
When you call entry.getIn(['data', 'date']), it returns a JS date object. You can either call toString on it in your preview component, or you can set a format on the date field in your config.yml. If you set a format, the date will be saved as a string in the markdown, instead of a MD date.
Closing this, but please reach out in our community chat if you need any further assistance.
BTW - setting a format to date only works if you re-save the dates before applying this change.
Thanks @erquhart @tech4him1
Most helpful comment
@jonathanphz
When you call
entry.getIn(['data', 'date']), it returns a JS date object. You can either calltoStringon it in your preview component, or you can set aformaton the date field in yourconfig.yml. If you set aformat, the date will be saved as a string in the markdown, instead of a MD date.