Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How can I separate the JSX mapping logic from the mapped array containing components with props?

I have a component that maps over an array of components that have props passed into them, something along these lines:

const Component = (prop0, prop1, prop2) => {

  const array = [
    {
      id: 0,
      component: <SubComponent0 prop0={prop0}>
    },
    {
      id: 1,
      component: <SubComponent1 prop1={prop1}>
    },
    {
      id: 2,
      component: <SubComponent2 prop2={prop2}>
    }
  ];

  return (
    {array.map(obj => 
      <div key={obj.id}>
        {obj.component}
      </div>
    )}
  );
};

export default Components;

I would like to separate the array into its own JS file that I import in, which worked fine for me in other cases where the components involve no props, but in this case I run into the problem of the props being undefined outside of the scope of this functional component, and I can't figure out how I can import/pass those props into the new JS file where I put the array. Is that possible to do, or is there another way for me to move the array into a separate file? Thanks.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

An approach to that could be to use a default function that builds the data and then returns everything.

data.js

const getData = (prop0, prop1, prop2) => {
  return [
  {
    id: 0,
    component: <SubComponent0 prop0={prop0}>
  },
  {
    id: 1,
    component: <SubComponent1 prop1={prop1}>
  },
  {
    id: 2,
    component: <SubComponent2 prop2={prop2}>
  }
];
}

module.exports = getData;

App.js

const getData = require('data.js');
const Component = (prop0, prop1, prop2) => {

  const array = getData(prop0, prop1, prop2);

};
about 4 years ago · Santiago Trujillo Report

0

I think you can also try Dynamic component means Component name from String I never implemented but I search little and prepare solution as below. You can try and let us know if it works

data.js

import { lazy } from 'react';
const getData = (props) =>{

const array = props.map((item,index) => {
    const DynamicComponent = lazy(() => import(`./SubComponent${index}`));
    return {
        id : index,
        component : <DynamicComponent prop=`prop${item}` />
    }
});

return array
}
export default getData;

App.js

import getData from './data.js';

const Component = (...props) => {

const array = getData(props)

return (
{array.map(obj => 
  <div key={obj.id}>
    {obj.component}
  </div>
)}
);
};

export default Component;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!