Comments (7)
// @ts-ignore
is your friend.
from react-portabletext.
This is how I did it:
import type { PortableTextBlock } from '@portabletext/types'
export type Recipe = {
_id: string
title: string
tags?: [{ title: string; slug: { current: string } }]
slug: { current: string }
body: [PortableTextBlock] // <-- 🎉
mainImage: {
asset: {
url: string
}
}
}
from react-portabletext.
Agreed. Anything in particular you're curious about?
The demo has some typescript code examples that might be useful in the meantime :)
from react-portabletext.
Well, mostly how I should type the block content I pass in :) I see in the demo app there's a repo called @portabletext/types
- would be great to have a link to this in the docs, for instance :)
from react-portabletext.
I struggled with this for a good while. I'm still not sure I fully understand it, that being said I have auto completion so I'm happy. I also had a pretty simple case, in that I wanted to style my text blocks with Chraka.
Example type definitions I create, I'm sure there is a better way to do this with the included types but I couldn't figure it out.
type Block = {
_key: string;
_type: string;
}
type BlockChild = Block & {
marks: string[];
text: string;
}
export type BlockDefault = Block & {
children: BlockChild[];
style: string;
}
//My object shape in sanity.
type Author = SanityDocument & {
name: string;
picture: string;
bio: BlockDefault;
image: Image;
projects: any[];
}
In my component I pass bio
to <PortableText />
pass in which custom component to render for the type block
.
<PortableText
value={bio}
components={{
types: {
block: PortableBioBlock,
},
}}
/>
My custom component PortableBioBlock
looks like this:
import { Text } from "@chakra-ui/react";
import type { PortableTextTypeComponent } from "@portabletext/react";
import { BlockDefault } from "src/@types/Post";
export const PortableBioBlock: PortableTextTypeComponent<BlockDefault> = ({
children,
}) => {
return (
<Text fontSize={"1.2rem"} letterSpacing={1.3} lineHeight={1.4}>
{children}
</Text>
);
};
I'm not sure if this is any help, or if there are ways I could improve it
from react-portabletext.
I did something along these lines which worked nice for our use-case:
interface SanityResponse extends SanityDocument {
beskrivelse: string;
embeddedInnhold: PortableTextBlock[];
maal: string;
tittel: string;
}
function MyComponent() {
const data: SanityResponse[] = await sanity.fetch(
'*[_type == "Aktivitet"]'
);
return (
<PortableText
value={data.map(({ embeddedInnhold: innhold }) => ({ innhold }))}
components={portableTextComponents}
/>
);
}
I can write a simple PR updating the readme on simple typescript use/gotchas if preferable :)
from react-portabletext.
We just added a "typing portable text" section to the README, let me know if it is helpful https://github.com/portabletext/react-portabletext#typing-portable-text
from react-portabletext.
Related Issues (20)
- Build Fail due to ReferenceError: createElement is not defined HOT 6
- unknownBlockStyle is not working / does nothing
- Children prop no longer available in custom block content HOT 1
- Marks Not Rendering HOT 1
- Issue passing client components on Next.js v14.* HOT 2
- typescript example blog rendering of audio player and code block HOT 1
- Can't do custom components HOT 3
- Cannot render elements with `type` of `text` or `span` using `types` HOT 4
- Can't get item position a list HOT 1
- Dependency Dashboard
- Anchored Headings for Portable Text HOT 2
- Version 2.0.1 breaks backwards compatibility HOT 3
- Can't Get Component to work HOT 1
- ReactServerComponentsError when using with Dynamic Routing in Next.js 13.4 App Router configuration HOT 1
- Nested Lists? HOT 2
- react-portable-text has no property 'toPlainText" HOT 4
- PortableText Typescript Error HOT 5
- Cannot render embedded image HOT 2
- [Feature request] Add a component wrapper prop
- Issues using with Nextjs and tailwind HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-portabletext.