Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does React dev tools show my component as Anonymous?

React dev tools works perfectly (properly shows the name of the component in components tab) when you have something like:

const MyComponent = ...
export { MyComponent }

But if you change it to inline exporting:

export const MyComponent = ...

it displays the component name as Anonymous.

Is something wrong with inline exporting in general?

like image 864
Boris Parfenenkov Avatar asked Mar 23 '20 18:03

Boris Parfenenkov


People also ask

What do the React Developer Tools show us?

Available for both Chrome and Firefox, the React Developer Tools are an essential instrument you can use to inspect a React application. They provide an inspector that reveals the React components tree that builds your page, and for each component you can go and check the props, the state, hooks, and lots more.

How do I connect React to dev tools?

Opening React Dev Tools To open the extension. Right-click anywhere in the browser window and click on Inspect. Additionally, you can open it by pressing F12. This will open browser developer tools with all the usual tabs like Elements, Console, etc.

Why not use anonymous functions in React?

Because we are passing anonymous functions, React will always re-render since it receives a new anonymous function as a prop which it is unable to compare to the previous anonymous function (since they are both anonymous). On the other hand, passing in a reference to the method like onClick={this.

How do you see React components in inspect?

Open the console by either right-clicking and inspecting an element or by opening the toolbar by clicking View > Developer > JavaScript console. The Components tab will show the current React component tree, along with any props, state, or context.


2 Answers

For inline exporting you need to manually specify the displayName property (I know, it's a pain).

So you do

    export const MyComponent = () => {
      //stuff happens here
    }

    MyComponent.displayName = "MyComponent";

like image 165
Iskandar Reza Avatar answered Nov 01 '22 19:11

Iskandar Reza


If it's still not resolved as it wasn't for me, I found a workaround, I added -

devtool: 'eval-cheap-module-source-map'

to my webpack.config.js and make sure to start the webpack build after adding the property.

Before adding the devtool. Before

After adding the devtool. After

source: https://webpack.js.org/configuration/devtool/

like image 31
Shek Avatar answered Nov 01 '22 18:11

Shek