Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

return of a function inside a render method is undefined

I have a code that kinda looks like this: (I’m omitting some things but…)

render() {
(bunch of props and state)
 return (
  <div>
   <CustomTabs>
    {this.renderTabs()}
   </CustomTabs>
  </div>
}

Where the renderTabs function looks like this:

 renderTabs = () => {
   const { apps } = this.props;
   apps.filter(app => app?.id !== 'Dashboard').map((app, key) => {
     return <CustomTab label={app.id} key={key} />;
   });
  }

According to some console logs, the filter and mapping is working, so the label and the key exists and are set properly. But the return of the function is undefined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I note that I’m using MUI datatables to do this and that if I delete the function and add multiple by hand instead, it works properly.

Why is this happening? I have lots of code that looks like this and lots of render methods that call another function to help the rendering and they work without problem.

>Solution :

You need to return filtered items:

renderTabs = () => {
   const { apps } = this.props;
   return apps.filter(app => app?.id !== 'Dashboard').map((app, key) => {
     return <CustomTab label={app.id} key={key} />;
   });
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading