简体   繁体   English

三元运算符错误...必须返回有效的 React 元素?

[英]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.

相关问题 错误:必须返回有效的React元素(或为null)(已检查Return()和Render()) - Error: A valid React element (or null) must be returned (Checked Return() and Render()) '必须返回一个有效的React元素(或null)'与ReactJS中的一个组件一起出错 - 'A valid React element (or null) must be returned' error with one of the component in ReactJS 必须返回有效的React元素(或null),但不能返回 - A valid React element (or null) must be returned but not 必须返回有效的React元素(或null) - valid React element (or null) must be returned 为什么显示此错误:“必须返回有效的React元素(或null)。 您可能返回了未定义的内容” - Why this error is showing: “A valid React element (or null) must be returned. You may have returned undefined” React.js必须返回有效的React元素(或null) - React.js A valid React element (or null) must be returned 反应三元运算符错误 - React ternary operator error “必须返回一个有效的React元素(或null)。”with array.map - “A valid React element (or null) must be returned.” with array.map 无状态组件:必须返回有效的React元素(或null) - Stateless component: A valid React element (or null) must be returned MuiThemeProvider.render():必须返回有效的React元素(或null) - MuiThemeProvider.render(): A valid React element (or null) must be returned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM