Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Dev tools show my Component as Unknown

I have the following simple component:

import React from 'react' import '../css.scss'  export default (props) => {   let activeClass = props.out ? 'is-active' : ''   return (     <div className='hamburgerWrapper'>       <button className={'hamburger hamburger--htla ' + activeClass}>         <span />       </button>     </div>   ) } 

When I look for it in the react dev tools, I see:

enter image description here

Is this because I need to extend React component? Do I need to create this as a variable and do so?

like image 450
Sequential Avatar asked Apr 17 '17 20:04

Sequential


People also ask

How do I show components in dev tools?

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

This happens when you export an anonymous function as your component. If you name the function (component) and then export it, it will show up in the React Dev Tools properly.

const MyComponent = (props) => {} export default MyComponent; 
like image 117
Michael Jasper Avatar answered Sep 19 '22 23:09

Michael Jasper


I realise the original question has been correctly answered already, but I just wanted to note a very similar issue you may encounter if using React.memo() or similar function. Consider the following code:

const MyComponent = React.memo(props => <div>Hello</div>) export default MyComponent 

The component will still display as Anonymous in devtools and certain React error messages (e.g. prop-types validation).

Ensuring that the Component is defined before trying to memoise it resolves this issue, e.g:

const MyComponent = props => <div>Hello</div> export default React.memo(MyComponent) 
like image 42
Matt Carr Avatar answered Sep 17 '22 23:09

Matt Carr