简体   繁体   English

关于React组件的ESLint规则:方法渲染预期没有返回值

[英]ESLint Rule on React Component: Method Render expected no return value

I can't for the life of me understand why anyone would even want this rule let alone what it means. 我终生无法理解为什么有人甚至想要这个规则,更不用说它的含义了。 I want to return something here in the render 在渲染中返回一些东西

export default class SearchTabs extends Component {
  render() {
    const { panels, selectedTab } = this.props;
    if (!panels || panels.length === 0) return;

    let filter = null;

    const tabs = panels.member.map((panel, idx) => {
      const { id: panelId, headline } = panel;
      const url = getHeaderLogo(panel, 50);
      const item = url ?
        <img src={url} alt={headline} /> : headline;
      const classname = classNames([
        searchResultsTheme.tabItem,
        (idx === selectedTab) ? searchResultsTheme.active : null,
      ]);

      filter = (idx === selectedTab) ? this.renderFilters(panel) : filter;

      return (
        <TabItem
          classname={`${classname} search-tab`}
          headline={headline}
          idx={idx}
          content={item}
          onclick={() => {
            this.tabChanged(idx, headline);
          }}
          panelId={panelId}
        />
      );
    });

    return (
      <div className={searchResultsTheme.filters}>
        <ul className={`${searchResultsTheme.tabs} ft-search-tabs`}>{tabs}</ul>
        <div className={searchResultsTheme.dropdown}>{filter}</div>
      </div>
    );
  }
}

I believe your problem is the line 我相信你的问题是线

if (!panels || panels.length === 0) return;

It's complaining because this code path does not return anything. 抱怨是因为此代码路径未返回任何内容。 Try changing it to 尝试将其更改为

if (!panels || panels.length === 0) return null;

Its because of this, if (!panels || panels.length === 0) return; 因此, if (!panels || panels.length === 0) return; . EsLint warns if return types are ambiguous because this returns undefined and later a ReactDom object. 如果返回类型不明确,EsLint会发出警告,因为这将返回未定义的值,并随后返回一个ReactDom对象。

simply if (!panels || panels.length === 0) return null; if (!panels || panels.length === 0) return null; and it should be fine. 而且应该没问题。

Regardless the other answers - the problem is caused by the consistent-return eslint, which makes sure that every function that you have will always have the same return option: 不管其他答案是什么,问题都是由consistent-return eslint引起的,这确保您拥有的每个函数始终具有相同的返回选项:

function doSomething(condition) {

    if (condition) {
        return true;
    } else {
        return;                   /*error Expected a return value.*/
    }
}

function doSomething(condition) {

    if (condition) {
        return;
    } else {
        return true;              /*error Expected no return value.*/
    }
}

function doSomething(condition) { /*error Expected to return a value at the end of this function.*/

    if (condition) {
        return true;
    }
}

It makes sense in languages like javascript that you can't force a function to return specific type/make sure it always return something (not undefined) and such. 在像javascript这样的语言中,您不能强制某个函数返回特定类型/确保它总是返回某些内容(不是未定义的内容),等等。

More information can be found here: 更多信息可以在这里找到:
https://eslint.org/docs/2.0.0/rules/consistent-return https://eslint.org/docs/2.0.0/rules/consistent-return

In your specific code - you have 3 return options. 在您的特定代码中-您有3个返回选项。 2 of them return jsx and 1 return undefined, which caused this specific eslint-error. 其中2个返回jsx,1个返回undefined,这导致此特定的eslint错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM