React.memo使用报错Component definition is missing display name

函数组件使用React.memo对其进行优化时,Eslint校验报错:Component definition is missing display 

const TestCom= React.memo(({ placement }) => {
  ...
});

export default TestCom;

原因:直接导出箭头函数不会给组件一个displayName

解决:

1、添加 TestCom.displayName = 'TestCom'

const TestCom= React.memo(({ placement }) => {
  ...
});

TestCom.displayName = 'TestCom';
export default TestCom;

2、导出一个常规函数,函数名为displayName

const TestCom= React.memo(function TestCom({ placement }) {
  ...
});

export default TestCom;

你可能感兴趣的:(react.js,前端,前端框架)