Discussion about using card parameters and custom block types in a React component.
61 replies
Last updated: May 29, 2022
N
hi, i dont understand why i cant use my card parameter in my react component
{cards.map((card, index) => { return <Card position={{ top: "top-[200px]", left: "left-0" }}> <PortableText value={data[0].card} components={portraitPortableTextComponents} /> </Card> })}
May 28, 2022, 8:48 AM
K
Without sharing an error, an issue, your schema, more code or some additional information, there is not much we can help with… 🙂
May 28, 2022, 8:50 AM
N
i have no error my ide simply doesnt let me use the card argument in my function
May 28, 2022, 8:51 AM
K
What do you mean “doesn’t let you use”? Your IDE is not your boss, you write the code. 😹
May 28, 2022, 8:52 AM
N
well he kinds of know better than me what i can or cannot di
May 28, 2022, 8:52 AM
N
{cards.map((card, index) => { return <Card position={{ top: card, left: "left-0" }}> <PortableText value={data[0].card} components={portraitPortableTextComponents} /> </Card> })}
May 28, 2022, 8:53 AM
K
Can you share a screenshot?
May 28, 2022, 8:53 AM
N
for the first card parameter in the CArd component i cant use my card parameter
May 28, 2022, 8:55 AM
K
I mean, there is nothing from what I can see that “doesn‘t let you use” that variable?
May 28, 2022, 8:55 AM
K
I still don’t understand what you mean with “can’t use.” It’s undefined or empty? Is it what you mean?
May 28, 2022, 8:55 AM
N
but not inside my portabletext
May 28, 2022, 8:55 AM
N
my ide doesnt show it as a parameter
May 28, 2022, 8:56 AM
K
Sorry?
May 28, 2022, 8:57 AM
K
I’m sorry, I genuinely do not understand what you mean, and the screenshot you shared doesn’t illustrate what problem you are facing with your IDE. 😞
May 28, 2022, 8:57 AM
N
alright
May 28, 2022, 8:59 AM
K
Looking at that screenshot, the
cardand
indexarguments are dimmed because they are not used within that closure.
May 28, 2022, 8:59 AM
N
i used card in the portable text
May 28, 2022, 9:00 AM
K
Not that I can see, no. You use a different variable:
data[0].card. That might not be the same as the
cardargument.
May 28, 2022, 9:01 AM
N
alright i see
May 28, 2022, 9:02 AM
K
Right now, your code shows that:• You iterate over a
cardsvariable, which is therefore an array.• For each item in the
cardsarray, you render the exact same JSX chunk, since you use neither the
cardargument nor the
indexargument.• Your portable text will always be rendered with the same thing right now.
May 28, 2022, 9:02 AM
N
i was hoping to get a different value from the different cards i have$
May 28, 2022, 9:03 AM
K
I can’t really help without knowing more about your schema. What is
cards? What is
data?
May 28, 2022, 9:04 AM
N
i solved my problem i was going on a dead end
May 28, 2022, 2:52 PM
N
i opted for this
<Card _position_={{ top: "top-[350px]", left: "left-0" }}>
<PortableText
_value_={data[0].card1}
_components_={portraitPortableTextComponents}
/>
</Card>
<Card _position_={{ top: "top-[200px]", right: "-right-0" }}>
<PortableText
_value_={data[0].card2}
_components_={portraitPortableTextComponents}
/>
</Card>
<Card _position_={{ top: "top-[440px]", right: "-right-[0px]" }}>
<PortableText
_value_={data[0].card3}
_components_={portraitPortableTextComponents}
/>
</Card>
May 28, 2022, 2:53 PM
N
if i want to add a card ill just have to uopdate the value of the portable text
May 28, 2022, 2:54 PM
N
and update my schema
May 28, 2022, 2:55 PM
N
schema :
{
name: "card1",
title: "Texte personnalisé",
type: "array",
of: [
{
type: "block",
// Only allow these block styles
styles: [
{ title: "h1", value: "h1" },
{ title: "h2", value: "h2" },
{ title: "h3", value: "h3" },
{ title: "h4", value: "h4" },
{ title: "h5", value: "h5" },
{ title: "h6", value: "h6" },
],
marks: {
decorators: [
{ title: "strong", value: "strong" },
{ title: "em", value: "em" },
{ title: "strike", value: "strike" },
{ title: "underline", value: "underline" },
{ title: "blue", value: "blue" },
{ title: "green", value: "green" },
{ title: "animation", value: "animation" },
{ title: "muted", value: "muted" },
],
},
// Only allow numbered lists
},
],
},
{
name: "card2",
title: "Texte personnalisé",
type: "array",
of: [
{
type: "block",
// Only allow these block styles
styles: [
{ title: "h1", value: "h1" },
{ title: "h2", value: "h2" },
{ title: "h3", value: "h3" },
{ title: "h4", value: "h4" },
{ title: "h5", value: "h5" },
{ title: "h6", value: "h6" },
],
marks: {
decorators: [
{ title: "strong", value: "strong" },
{ title: "em", value: "em" },
{ title: "strike", value: "strike" },
{ title: "underline", value: "underline" },
{ title: "blue", value: "blue" },
{ title: "green", value: "green" },
{ title: "animation", value: "animation" },
{ title: "muted", value: "muted" },
],
},
// Only allow numbered lists
},
],
},
{
name: "card3",
title: "Texte personnalisé",
type: "array",
of: [
{
type: "block",
// Only allow these block styles
styles: [
{ title: "h1", value: "h1" },
{ title: "h2", value: "h2" },
{ title: "h3", value: "h3" },
{ title: "h4", value: "h4" },
{ title: "h5", value: "h5" },
{ title: "h6", value: "h6" },
],
marks: {
decorators: [
{ title: "strong", value: "strong" },
{ title: "em", value: "em" },
{ title: "strike", value: "strike" },
{ title: "underline", value: "underline" },
{ title: "blue", value: "blue" },
{ title: "green", value: "green" },
{ title: "animation", value: "animation" },
{ title: "muted", value: "muted" },
],
},
// Only allow numbered lists
},
],
},
May 28, 2022, 2:57 PM
N
let me know if i can make it better but im quite satisfied with it
May 28, 2022, 2:57 PM
T
If
You can create an array of cards instead, and get the data with
[EDIT] if you want to limit the number of items in your array, you can add validation rules.
card1,
card2and,
card3have the same block type, I don’t see why you need to duplicate them 🤔.
You can create an array of cards instead, and get the data with
cards[index]
[EDIT] if you want to limit the number of items in your array, you can add validation rules.
{ title: 'Cards', name: 'cards', type: 'array', of: [{ type: 'block' }], validation: Rule => Rule.max(3) // 3 items max. }
May 28, 2022, 3:26 PM
T
if your cards need to contain more information, you can create a card object (singular) and do:
{ title: 'Cards', name: 'cards', type: 'array', of: [{ type: 'card' }] }
export default { title: 'Card', name: 'card', type: 'object', fields: [ { title: 'Title', name: 'title', type: 'string', }, { title: 'Content', name: 'content', type: 'cardBlock', // custom portableText }, { // more fields... } ] }
May 28, 2022, 3:34 PM
N
excuse me but i don't see the point about this line of code
since in my editor i only have one rich text field
{ title: 'Cards', name: 'cards', type: 'array', of: [{ type: 'block' }], validation: Rule => Rule.max(3) // 3 items max. }
May 28, 2022, 4:16 PM
N
i'd like to have 3
May 28, 2022, 4:20 PM
N
or more
May 28, 2022, 4:20 PM
N
when i see
type: 'cardBlock'I don't understand either since i thought you could only set it to block
May 28, 2022, 4:22 PM
N
u mean i should replace all my cards1 cards2 cards3 with
{ title: 'Cards', name: 'cards', type: 'array', of: [{ type: 'card' }] }
May 28, 2022, 4:24 PM
N
what will differentiate card 1 from card 2 from card 3 if i only have one rich text ? and get the correct index ?
May 28, 2022, 4:29 PM
T
An array let you create any number of
then you can reuse it elsewhere:
cardyou want, making it more reusable, rather than hardcoding 3 cards.
cardBlockwas an example, but if you want to reuse your
block, I’d suggest to create a custom block type:
export default { title: 'Custom PortableText Block', name: 'customPortableTextBlock', type: 'array', of: [ { // add your configuration here type: 'block', styles: [], lists: [], marks: { decorators: [], annotations: [], }, }, ], }
{ title: 'Main Description', name: 'mainDescription', type: 'customPortableTextBlock' }, { title: 'Author Description', name: 'authorDescription', type: 'customPortableTextBlock' }
May 28, 2022, 6:14 PM
N
when you say i can use it elsewhere where in which document do you make reference ?
May 28, 2022, 7:08 PM
N
in another schema ?
May 28, 2022, 7:08 PM
N
alright i understood
May 28, 2022, 7:15 PM
N
i have one last question
May 28, 2022, 7:16 PM
N
is it possible to have richtext inputs only on certain documents ?
May 28, 2022, 7:16 PM
N
or schemas are always the same for all the documents of a certain types
May 28, 2022, 7:17 PM
N
anyway thank you all very much for your help im grateful
May 28, 2022, 7:18 PM
K
If you make your field not required, then it can be filled on some document and not on others.
May 28, 2022, 7:45 PM
N
good to know. But for the moment i will make a schema for each section so each field will be required
May 28, 2022, 7:52 PM
N
thanks for the info
May 28, 2022, 7:52 PM
N
I have an error when i try to add multple cards
May 28, 2022, 9:14 PM
N
when i click on te block component i get an error
May 28, 2022, 9:17 PM
N
same error when i press the add item button
May 28, 2022, 9:56 PM
N
_export_ default {
title: "Card",
name: "card",
type: "object",
fields: [
{
title: "Title",
name: "title",
type: "string",
},
{
title: "Content",
name: "content",
type: "block", _// custom portableText_
},
],
}
May 28, 2022, 9:58 PM
N
{
title: "Cards",
name: "cards",
type: "array",
of: [{ type: "card" }],
},
May 28, 2022, 9:58 PM
N
i(m suspicioous about this line
type: "block", _// custom portableText_
May 28, 2022, 10:00 PM
N
i know how to use portabletext on the frontend but have no clue on the sanity side
May 28, 2022, 10:00 PM
T
You need a custom block type here. You can refer to my previous example with the customPortableTextBlock. And of course this can be named whatever you want.
May 29, 2022, 7:53 AM
N
man you helped me so much
May 29, 2022, 8:21 AM
N
sanity is way more clear for me
May 29, 2022, 8:21 AM
N
i'm having a blast devlopping with it
May 29, 2022, 8:22 AM
N
learning nextjs along with it so much to digest
May 29, 2022, 8:22 AM
N
😀
May 29, 2022, 8:23 AM
N
✅
May 29, 2022, 8:23 AM
T
Glad it helped 👍
May 29, 2022, 9:12 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.