GithubHelp home page GithubHelp logo

TypeScript usage? about react-portabletext HOT 7 OPEN

portabletext avatar portabletext commented on September 26, 2024
TypeScript usage?

from react-portabletext.

Comments (7)

eldoy avatar eldoy commented on September 26, 2024 10

// @ts-ignore is your friend.

from react-portabletext.

ottob avatar ottob commented on September 26, 2024 2

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.

rexxars avatar rexxars commented on September 26, 2024 1

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.

selbekk avatar selbekk commented on September 26, 2024

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.

swalker326 avatar swalker326 commented on September 26, 2024

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.

tanettrimas avatar tanettrimas commented on September 26, 2024

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.

snorrees avatar snorrees commented on September 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.