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

create array of JSX elements and pass prop to each element

i am a new in react framework.
what i want to do is to create array of JSX elements (Components)
for examples:

import Intro from '../Introduction/Intro';
import Skills from '../Skills/Skills';

export const SECTIONS = [<Intro/>, <Skills/>]

const SomeComponent = () => {
const handler = () => {...}
return (
   <div className={classes.container}>
     <div className={classes.fields}>
       {SECTIONS.map((section, i) => {
        return <Fragment key={i}>{section /* for each element i want also pass prop */ }</Fragment>
     })}
     </div>
    <div className={classes.check}>...</div>
  </div>
   ) 
}

so i want to pass handler function to each element in my array

(<Intro foo={handler}/>)

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

Is it possible to do this?

>Solution :

yes you can do this :

  export const SECTIONS = [Intro, Skills]
  const SomeComponent = () => {
    const handler = () => {...}
    return (
      <div className={classes.container}>
        <div className={classes.fields}>
          {SECTIONS.map((Section, i) => {
            return <Fragment key={i}><Section foo={handler}></Section></Fragment>
          })}
        </div>
        <div className={classes.check}>...</div>
      </div>
    )
  }
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