我试图获得我的标题的长度,但它说.长度未定义,它在我登录时返回长度



这是im调用caption的地方,caption是im传递给另一个组件的道具btw

///i passed caption here as a prop
const PostsHeader = ({ image, username, postimage, caption }) => {
///number of likes
const likes = Math.trunc(Math.random() * 200);
return (
<Container>
<Div>
<Left>
<Img
style={{
backgroundImage: `url(${require(`../../img/${image}`)})`,
}}
/>
<UName>{username}</UName>
</Left>
<Right>
<UilEllipsisH size={20} style={{ marginRight: 10, marginTop: 10 }} />
</Right>
</Div>
<PostImg
style={{
backgroundImage: `url(${require(`../../img/${postimage}`)})`,
}}
/>
<Icons>
<LeftIcons>
<UilHeart size={22} style={{ marginRight: 10, cursor: "pointer" }} />
<UilComment
size={22}
style={{ marginRight: 10, cursor: "pointer" }}
/>
<UilMessage
size={22}
style={{ marginRight: 10, cursor: "pointer" }}
/>
</LeftIcons>
<RightIcons>
<UilBookmarkFull
size={22}
style={{ marginRight: 3, cursor: "pointer" }}
/>
</RightIcons>
</Icons>
<Likes>{likes} Likes</Likes>
//comment, this is where im using the caption
<Caption>
<b style={{ fontWeight: 600, paddingRight: 5 }}>Tommyk</b>
{caption.length > 61 ? caption.slice(0, 61) + "..." : caption}
</Caption>
</Container>
);
};
export default PostsHeader;

这是我的字幕数组和我使用字幕道具的地方

const Posts = () => {
const captions = [
"Whatever is good for your soul, do that Whatever is good for your soul, do that",
"Even the stars were jealous of the sparkle in her eyes",
"Stress less and enjoy the best",
"Get out there and live a little",
"I’m not high maintenance, you’re just low effort",
"I’m not gonna sugar coat the truth, I’m not Willy Wonka",
"Life is better when you’re laughing",
"Look for the magic in every moment",
"Vodka may not be the answer but it’s worth a shot",
"A sass a day keeps the basics away",
];
console.log(captions[0].length);
return (
<Container>
<PostsHeaderAndBody
image="tk.jpg"
username="The.tommyk"
postimage="back.jpg"
caption={captions[0]}
/>
</Container>
);
};
export default Posts;

现在的问题是{caption.length > 61 ? caption.slice(0, 61) + "..." : caption}说没有定义,但如果我是console.log(captions[0].length);,它会记录标题的长度,所以我不知道为什么标题长度没有定义,请帮助

我已经解决了这个问题。问题是我在'中添加了撇号,这显然是不允许的

不是这样```常量标题=["无论对你的灵魂有什么好处,都要这样做无论对你灵魂有什么好,都要那样做","就连星星也嫉妒她眼睛里的闪光";,"少一点压力,享受最好的";,"走出去,过一点";,"我不是高维护,你只是低付出","我不会粉饰事实,我不是Willy Wonka","当你笑的时候,生活会更好","寻找神奇的每一刻";,"伏特加可能不是答案,但它值得一试";,"一天一个时髦,远离基本";,];


>I removed the apostrophes like this ```const captions = [
"Whatever is good for your soul, do that Whatever is good for your soul, do that",
"Even the stars were jealous of the sparkle in her eyes",
"Stress less and enjoy the best",
"Get out there and live a little",
"Im not high maintenance, youre just low effort",
"Im not gonna sugar coat the truth, Im not Willy Wonka",
"Life is better when youre laughing",
"Look for the magic in every moment",
"Vodka may not be the answer but its worth a shot",
"A sass a day keeps the basics away",
];

最新更新