admin管理员组文章数量:1399509
I've tried several methods in setting the display name for my React Component, but none has worked: I tried to set it as a public static variable like this:
class Base extends React.Component<any, any>{
public static displayName = "Base";
constructor(props){
...
}
render(){
...
}
}
But eslint still throws me this error:
error Component definition is missing display name react/display-name
I tried an alternative approach where I set it outside the class definition:
class Base extends React.Component<any, any>{
constructor(props){
...
}
render(){
...
}
}
Base.displayName = "Base";
And I end up getting an error saying:
Property 'displayName' does not exist on type 'typeof Base'.
I've tried different methods from other Stackoverflow posts but I can't seem to get rid of the error. How can I resolve this? Please help.
edit: answered my own question below. The core of this problem seemed to be regarding anonymous functions rather than the React class.
I've tried several methods in setting the display name for my React Component, but none has worked: I tried to set it as a public static variable like this:
class Base extends React.Component<any, any>{
public static displayName = "Base";
constructor(props){
...
}
render(){
...
}
}
But eslint still throws me this error:
error Component definition is missing display name react/display-name
I tried an alternative approach where I set it outside the class definition:
class Base extends React.Component<any, any>{
constructor(props){
...
}
render(){
...
}
}
Base.displayName = "Base";
And I end up getting an error saying:
Property 'displayName' does not exist on type 'typeof Base'.
I've tried different methods from other Stackoverflow posts but I can't seem to get rid of the error. How can I resolve this? Please help.
edit: answered my own question below. The core of this problem seemed to be regarding anonymous functions rather than the React class.
Share Improve this question edited Apr 20, 2020 at 17:46 avhhh asked Apr 20, 2020 at 16:31 avhhhavhhh 2711 gold badge5 silver badges18 bronze badges 6- I don't think this error es from those examples, please make a producible one: How to create a Minimal, Reproducible Example – Dennis Vash Commented Apr 20, 2020 at 16:36
- "Usually, you don’t need to set it explicitly because it’s inferred from the name of the function or class that defines the ponent." – jonrsharpe Commented Apr 20, 2020 at 16:37
- Base.name maybe could work? – Black Hole Commented Apr 20, 2020 at 16:38
- @BlackHole That also does not work. Gives the error Property 'name' does not exist on type 'typeof Base'. – avhhh Commented Apr 20, 2020 at 16:47
- @avhhh Allright, in javascript you can get the class name with Object methods, but it dont work i dont know – Black Hole Commented Apr 20, 2020 at 16:50
3 Answers
Reset to default 5Instead of using public static displayName = "Base";
remove public and use it like static displayName = "Base";
class Base extends React.Component<any, any>{
static displayName = "Base";
constructor(props){
...
}
render(){
...
}
}
Answering my own question here. It turned out that the problem lies in a different area than I had originally thought. The eslint error Component definition is missing display name react/display-name
was indicated where I used an anonymous function to return the React Component:
export function renderForm(){
return {
react: () => <Base />
}
}
I thought that it was saying that <Base/>
needed a displayName, but it turned out that the problem is the unnamed function. I resolved it by naming that function:
export function renderForm(){
return {
react: function renderComponent(){ return <Base />}
}
}
Not sure if this will help anyone else, but the eslint error is now gone!
edit: changing the rule, as mentioned from the other two answers is also a valid solution fyi
You are using this ESLint plugin I take it?
In that case, did you set ignoreTranspilerName
option to true
? If so, that could be your issue.
Why even have this rule if you already have names for your ponent? The transpiler should use the class name as the ponent name by default
本文标签: javascriptHow to set display name for a React class (ES6)Stack Overflow
版权声明:本文标题:javascript - How to set display name for a React class (ES6)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744205826a2595182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论