0

I created a helper routeData to map the data and pass the data to the child component <MyComponent />:

const routeData = {
  routeOne: {
    title: 'Title one',
    description: 'Description one',
    image: '/Header.jpg',
    style: config.style,
    id: '01',
  },
  routeTwo: {
    title: 'Title two',
    description: 'Description two',
    image: '/Header02.jpg',
    style: config.style02,
    id: '02',
  },
};

However now I have to add all this props like:

  <Route
    path={routeData.routeOne.path}
    render={() => (
      <MyComponent
        title={routeData.routeOne.title}
        description={routeData.routeOne.description}
        imageUrl={routeData.routeOne.image}
        link={routeData.routeOne.link}
        id={routeData.routeOne.id}
        // etc
      />
    )}
  />

Is there a better way to construct this helper and pass everything by one prop e.g.:

<Route path={routeData.routeOne.path} render={() => (   
  <MyComponent data={routeData.routeOne} /> )}
/> 
meez
  • 3,783
  • 5
  • 37
  • 91

1 Answers1

0

You just spread the object in you component like this

<MyComponent {...routeData.routeOne}/>

and inside MyCompoenet you can access them from props


function MyComponent (props){

  .
  .
  .
  return <> {props.title} </>

}

Ahmed Gaafer
  • 1,603
  • 9
  • 26