小编典典

为什么我们不能在响应中将布尔值作为prop传递,它总是要求在我的代码中传递字符串

reactjs

即使我已应用propType验证,但在传递hasvacancyprop的布尔值时,我的编辑器也会引发错误。这是我所看到的:

错误:“ SyntaxError:JSX值应为表达式或带引号的JSX文本”

我知道我正在为’hasvacancy’属性传递一个字符串类型的值,但是我需要做些什么,这样我就可以通过该属性传递一个布尔值或其他数据类型。

import React from 'react';
import { render } from 'react-dom';


class VacancySign extends React.Component{

  render() {
    console.log('------------hasvacancy------', this.props.hasvacancy);
    if(this.props.hasvacancy) {
      return(
        <div>
          <p>Vacancy</p>
        </div>
      );
    } else {
      return(
        <div>
          <p>No-Vacancy</p>
        </div>);
    }

  }
}

VacancySign.propTypes ={
  hasvacancy: React.PropTypes.bool.isRequired
}

render(<VacancySign hasvacancy='false'/> , 
document.getElementById('root'));

阅读 277

收藏
2020-07-22

共1个答案

小编典典

您应将布尔值括在{}中:

render(<VacancySign hasvacancy={false}/> , document.getElementById('root'));
2020-07-22