Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React组件检查变量为空_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript React组件检查变量为空

Javascript React组件检查变量为空,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我有一个react组件,其中类/组件中还有一个变量/ 代码如下: import React from 'react'; export class Header extends React.Component { constructor() { super(); this.myvar = 'some text'; } render() { return ( <div> <div>{this.myvar}&l

我有一个react组件,其中类/组件中还有一个变量/

代码如下:

import React from 'react';

export class Header extends React.Component {
  constructor() {
    super();
    this.myvar = 'some text';
  }

  render() {
    return (
      <div>
        <div>{this.myvar}</div>
      </div>

    );
  }
}
从“React”导入React;
导出类标题扩展了React.Component{
构造函数(){
超级();
this.myvar='some text';
}
render(){
返回(
{this.myvar}
);
}
}
如何检查“myvar”是否为空,以及上面的组件是否为空?

render(){
render() {
    return (
      <div>
        <div>{!!(this.myvar)?this.myvar:"whatever you want"}</div>//
      </div>

    );
  }
返回( {!!(this.myvar)?this.myvar:“你想要什么都行”}// ); }
:检查未定义、空和空值


提供的解决方案与eslint的规则相冲突

另一种让eslint高兴的方法如下所示:

render() {
    return (
      <div>
        <div>{!this.myvar ? "whatever you want" : this.myvar}</div>//
      </div>

    );
  }
render(){
返回(
{!this.myvar?“你想要什么都行”:this.myvar}//
);
}

在这种情况下定义“空”?空。。。完全没有文本为什么不使用React states?所以是空字符串?
this.myvar
是否会有一个非字符串的值?此外,React不提供任何工具来检查变量/值的“空性”。这只是JavaScript,所以请使用您通常使用的任何方法。我正在寻找一种可以在内联上触发的方法?@JakeBrown777:{!!(this.myvar)?this.myvar:func()}。这正是我需要的。我不明白这是怎么回事votes@JakeBrown777. Great.FWIW,这也将考虑<代码> 0 <代码>和<代码> false <代码>为“EMtPy”。