[英]Ternary operator error... a valid React element must be returned?
I've got this simple exercise on react components to work with an if conditional but I wanted to try using a ternary operator instead.我有一个关于反应组件的简单练习,以使用if 条件,但我想尝试使用三元运算符。 The problem is that now I get an error telling me: "TonightsPlan.render(): a valid React element must be returned..."
问题是现在我收到一条错误消息,告诉我: “TonightsPlan.render(): a valid React element must be returned...”
import React from 'react';
import ReactDOM from 'react-dom';
const fiftyFifty = Math.random() < 0.5;
// New component class starts here:
class TonightsPlan extends React.Component
{
render()
{
fiftyFifty ?
<h1>Tonight I'm going out WOO</h1>:
<h1>Tonight I'm going to bed WOOO</h1>;
}
};
ReactDOM.render(<TonightsPlan/>, document.body);
So I guess there's something wrong with the way I'm using the ternary operator here but I can't tell what it is... I could really use some help to figure that out, please!所以我想我在这里使用三元运算符的方式有问题,但我不知道它是什么......我真的可以使用一些帮助来解决这个问题,拜托!
Everything is fine, just the return in render is missing.一切都很好,只是缺少渲染中的返回。 J
Ĵ
import React from 'react';
import ReactDOM from 'react-dom';
const fiftyFifty = Math.random() < 0.5;
// New component class starts here:
class TonightsPlan extends React.Component
{
render()
{
return (
(fiftyFifty)
? <h1>Tonight I'm going out WOO</h1>
: <h1>Tonight I'm going to bed WOOO</h1>;
)
}
};
ReactDOM.render(<TonightsPlan/>, document.body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.