gpt4 book ai didi

javascript - JSX 中内联 if-else 和大块代码

转载 作者:行者123 更新时间:2023-12-03 04:19:47 26 4
gpt4 key购买 nike

我有 2 段代码想要在条件之间切换,并且我更喜欢内联执行,但似乎这种语法不正确:

const chunk1 = (...)
const chunk2 = (...)

{this.state.condition ? (
{chunk1}
) : (
{chunk2}
)}

我得到:

Uncaught Error: Objects are not valid as a React child (found: object with keys {loginOptions}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Card.

正确的方法是什么?

最佳答案

我认为你的语法是错误的。试试这个:

{this.state.condition ? 
chunk1
:
chunk2
}

或者

  if (condition) {
content = chunk1;
} else {
content = chunk2;
}

{content}

ES7 做:

const content = (data) => do {
if (data) chunk1
else chunk2
}

{content}

更多信息

Official React documentation for Conditional rendering

关于javascript - JSX 中内联 if-else 和大块代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com