-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Show proper names for stateless functional components #267
Comments
Internally (Facebook) I was seeing.
I'll generate a repro using public react to see if this is FB specific or not. |
I am seeing this issue as well using public react. Here is my code for that same root component:
|
What's your babel version? This is a babel issue I'm sure On Mon, Nov 2, 2015 at 2:24 PM Steve Szczecina notifications@github.com
|
I'm using Babelify 6.3.0 with Browserify |
Maybe you don't have the function.name transform enabled? We probably rely on that. |
Using browserify with coffee-reactify. Anything that I can do? |
@joaomilho make an issue w/ coffee-reactify asking them about getting |
Also see facebook/react#5618 |
For those using TypeScript you can do the following (example notice declare global {
interface Function {
displayName: string;
}
}
/********
*
* Primitives
*
********/
interface PrimitiveProps extends React.HTMLProps<HTMLDivElement>{};
/**
* Takes as much space as it needs, no more, no less
*/
export const Content = Radium((props: PrimitiveProps) => {
const style = csx.extend(props.style || {},csx.content);
return (
<div data-comment="Content" {...props} style={style}>
{props.children}
</div>
);
});
Content.displayName = "Content"; |
@basarat Old comment I know but hopefully this is useful for someone: It's possible to get this working automatically using this Babel plugin which will use the filename to assign the function a name if it does not have one: https://github.com/wyze/babel-plugin-transform-react-stateless-component-name/ Few things to watch out for:
This doesn't currently work (either in ES6 or Typescript) for SFCs wrapped in a higher order component, e.g. |
Oh, or even easier, you can do away with the plugin and just name your component by putting into a constant, then exporting that separately, for example:
Will show as Slightly more typing but probably clearer. Note this still doesn't seem to solve the HOC problem (already raised at: wyze/babel-plugin-transform-react-stateless-component-name#2) |
Ah (sorry to spam this thread!) – there is a way round the HOC problem, which is to do as the above and store the component in a named constant, then wrap it when exporting (rather than when creating), so instead of:
You have:
Which arguably reads better anyway :) (and allows you to export the non-decorated component if needed e.g. for testing) |
@tomduncalf Just ran into this problem myself!
Your fix worked until I turned mangling on with uglifyjs |
Closing. If you use Babel, es2015 preset contains function-name plugin that infers the name based on left hand side in expressions. For Facebook internal use case, @spicyj recently fixed it by enabling the same transform. |
I'm using TypeScript with:
And I just see |
As mentioned before : #267 (comment) 🌹 declare global {
interface Function {
displayName?: string;
}
}
export const MyComponent = ({text}) => (
<div>{text}</div>
);
MyComponent.displayName = 'MyComponent'; |
Thanks that does work... I suppose I was asking if there's a way to make it work without explicitly writing the I found that writing it this way seems to work:
|
Yup, on modern browsers, cause they have |
On second thought both are equivalent on chrome as it also adds const x = ()=> undefined; function y(){}; console.log(x.name,y.name); // x y
|
I think you're right about the
I get But if I do this:
I get |
@aaronbeall Please file a bug with Babel if Babel doesn't add implicit |
(Or with TypeScript if that's what you use) |
@gaearon Yep I'm using TypeScript. It doesn't look like TS emits a |
Just stumbled on this thread. I've been running into an issue where the React Dev Tools tell me my component is called // MyComponent.js
export default (props) => <someJSX>;
// App.js
import MyComponent from './MyComponent.js'; But this works: export default function MyComponent(props) {
return <someJSX>;
} In case that's useful for anyone. |
Yes, this is expected, because Babel only infers the arrow name if you assign it to something. const MyComponent = (props) => <someJSX />;
export default MyComponent; |
I could fix the problem in typescript project by setting |
@bordoley says they all show up as StatelessComponent.
The text was updated successfully, but these errors were encountered: