Switch语句中的意外令牌`switch'
我从以下代码中获得了意外的令牌switch
错误:
const bodyelements = () => (
body.map(item => (
switch (item?.typename) {
case 'ArticleBodyText':
return <TextBlock dangerouslySetInnerHTML={{ __html: item?.primary?.[0].text }} />
case 'ArticleBodyImage':
return <ArticleBodyImage item={item} />
default:
break;
)}
)
我已经尝试在Stackoverflow上查看错误,但是我看到的修复程序并不适用于我的代码。有人知道怎么了吗?
编辑:
我将其更改为此
const bodyelements = () => {
body.map(item => (
switch (item?.__typename) {
case 'ArticleBodyText':
return <TextBlock dangerouslySetInnerHTML={{ __html: item?.primary?.[0].text }} />
break;
case 'ArticleBodyImage':
return <ArticleBodyImage item={item} />
break;
default:
break;
)}
}
,但我仍然遇到相同的错误
I am getting the Unexpected token switch
error from the following code:
const bodyelements = () => (
body.map(item => (
switch (item?.typename) {
case 'ArticleBodyText':
return <TextBlock dangerouslySetInnerHTML={{ __html: item?.primary?.[0].text }} />
case 'ArticleBodyImage':
return <ArticleBodyImage item={item} />
default:
break;
)}
)
I already tried to look the error up on stackoverflow, but the fixes I saw didn't apply to my code. Does somebody know what's wrong?
EDIT:
I changed it to this
const bodyelements = () => {
body.map(item => (
switch (item?.__typename) {
case 'ArticleBodyText':
return <TextBlock dangerouslySetInnerHTML={{ __html: item?.primary?.[0].text }} />
break;
case 'ArticleBodyImage':
return <ArticleBodyImage item={item} />
break;
default:
break;
)}
}
But I am still getting the same error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
短函数语法有两个替代方法:
使用表达式并返回其结果:
使用一个或多个语句的块:
switch
是一个语句,因此您需要使用块变体。The short function syntax have two alternatives:
One that uses an expression and returns its result:
One that uses a block for one or more statements:
switch
is a statement, so you need to use the block variant.您在代码中遇到了这些问题,
尝试一下
You have these issues in your code
Try this