Where is SanityPreview component in Sanity v3?

9 replies
Last updated: Jan 6, 2023
HiI'm trying to port a custom tool from v2 to v3, and it uses the
<SanityPreview>
component. Where do I get this from in v3?

import React from "react";
import { SanityPreview } from "@sanity/base/preview";
import { Card, Box } from "@sanity/ui";

export default ({document, schemaType,})=>{
  const documentId = document._id;

  return (
    <Box paddingX={2} as="li">
      <Card
        state={{ selectedDocumentId: documentId }}
        padding={2}
        radius={2}
      >
        <SanityPreview layout="default" value={document} type={schemaType} />
      </Card>
    </Box>
  );
}
AI Update

I need to be honest - I couldn't find specific documentation about the direct replacement for SanityPreview from @sanity/base/preview in Studio v3. This is a common pain point during migration since the component was widely used in v2.

What I can confirm is that Studio v3 deprecated the entire Parts System, which means all @sanity/base/* imports no longer exist. Everything moved to the main sanity package with JavaScript-based configuration.

Here's what I'd recommend:

For rendering previews in custom tools/plugins in v3, you have a few approaches:

  1. Use Sanity UI components directly - Build your preview UI using @sanity/ui components and fetch the data you need through hooks or the client

  2. Check the sanity package exports - There may be a preview component exported from the main package, but I cannot verify the exact API without access to the current type definitions

  3. Use preview functionality from Structure Builder - If you're building custom views, the Component View system might provide what you need

My strong recommendation: Post this question in the Sanity Community Slack or check the official v2 to v3 migration guide. The Sanity team is very responsive to migration questions and can point you to the exact replacement component and its API.

Since you're porting a custom tool, the team can also advise on whether the v3 plugin architecture has better patterns for what you're trying to accomplish. The v3 plugin API is significantly different, so there might be a more idiomatic approach than directly replacing the component.

Sorry I couldn't provide the exact code replacement - I'd rather point you to verified sources than give you something that might not work!

Show original thread
9 replies
Are you using next js?
No, this is a sanity studio tool.&lt;SanityPreview&gt; is a react component that should return the same thing that is uses as a document list item in the deskTool.
Looks like you need a
defineConfig
object and run sanity build.
No this is not relevant.
// Single workspace configuration

import {defineConfig} from 'sanity'
import {deskTool} from 'sanity/desk'
import {schemaTypes} from './schemas'

export default defineConfig({
  projectId: '<projectId>',
	dataset: 'production',
  plugins: [deskTool()],
  schema: {
	  types: schemaTypes,
  },
})
I have found a solution using
<SanityDefaultPreview>
which gives me the desired result, albeit with a little more boilerplate required as I have to provide title and subtitle explicitly rather than relying on the preview definition in the document's schema (unless I am missing something here).

import React from "react";
import { Card, Box } from "@sanity/ui";
import { SanityDefaultPreview } from "sanity";

export default ({document})=>{

  return (
    <Box paddingX={2} as="li">
      <Card
        padding={2}
        radius={2}
      >
        <SanityDefaultPreview  layout={"default"} title={document.title} subtitle={document.subtitle} />
      </Card>
    </Box>
  );
}

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?