我知道displayName有时候需要设置它,特别是当你处理生产版本时.我想知道如何使用我的功能组件设置它 - 它是否可能/允许?
这是我在我的课程组件中得到的:
var MyComponent = React.createClass({
displayName: 'HeyHey',
render: function() {
console.log(this.displayName);
}
});Run Code Online (Sandbox Code Playgroud)
如何在无状态组件中执行相同的操作?
使用Reac.memo包裹我的功能组件,它可以流畅运行,但eslint总是让我想起了两个错误:
error Component definition is missing display name react/display-name
error 'time' is missing in props validation react/prop-types
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
type Data = {
time: number;
};
const Child: React.FC<Data> = React.memo(({ time }) => {
console.log('child render...');
const newTime: string = useMemo(() => {
return changeTime(time);
}, [time]);
return (
<>
<p>Time is {newTime}</p>
{/* <p>Random is: {children}</p> */}
</>
);
});
Run Code Online (Sandbox Code Playgroud)
我的整个代码:
import React, { useState, useMemo } from 'react';
const Father = () => {
const [time, …Run Code Online (Sandbox Code Playgroud)