创建一个具有布尔属性hasSeat的HouseFull组件

时间:2018-10-10 09:58:33

标签: reactjs

创建一个具有布尔属性hasSeat的HouseFull组件。该组件应根据道具用文本HouseFull或Vacant渲染div。

3 个答案:

答案 0 :(得分:1)

我猜这是您要寻找的?如果将prop hasSeat属性更改为true或false,则会得到所需的结果。

import React, { Component } from "react";
import ReactDOM from "react-dom";

 class HouseFull extends Component {
      render() {
        return (
           <div>{this.props.hasSeat ? "HouseFull" : "Vacant"}</div>
        );
      }
    }


class App extends Component {
  render() {
    return (
      <div>
        <HouseFull hasSeat={true} />
      </div>
    );
  }
}

export default App;

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

答案 1 :(得分:1)

这就是答案,您要寻找的

HouseFull.js

class HouseFull extends Component {
  render(){
      return(
            <div>
               <p>{this.props.hasSeat? 'Vacant': 'HouseFull'}</p>
            </div>
            );
          }
 }
 

Index.js

ReactDOM.render(<HouseFull hasSeat={true}/>, document.getElementById('root'))

答案 2 :(得分:0)

我观察到代码中的更改很小。只需交换条件,因为当您检查真实条件时,第一个表达式将首先计算。

import React, { Component } from "react";
import ReactDOM from "react-dom";

 class HouseFull extends Component {
      render() {

        return (
           <div>{this.props.hasSeat ? "Vacant" : "HouseFull"}</div>
        );
      }
    }


class App extends Component {
  render() {
    return (
      <div>
        <HouseFull hasSeat={true} />
        <HouseFull hasSeat={false} />
      </div>
    );
  }
}

export default App;

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);