Meriegg
Meriegg

Reputation: 107

How to transfer data from one file to another in reactjs

i have this array of objects, the thing that i am trying to do is to take the array from the file it's sitting in, and transfer it to another file where i can map the items. This is my code (the formatting and spacing is not on point) :

const Upload = () => {


    const photos = [
        {
            id: new Date().getMilliseconds().toString(),
            imagePath: "url..."
        },
    ];

    return (
    <>
       // Markup ....
    </>
    );
};

And i want to do something like this:

import photos from './COMPONENT_NAME';
const Func = () => {
    return (
      <> {photos.map((item) => <div> code... </div> )} </>
    );

Is there any way to do it?

Upvotes: 1

Views: 3960

Answers (1)

Yaya
Yaya

Reputation: 4848

You can simply export them.
First, move the list outside of the Upload function and then put export key before the declaration like so.

 export const photos = [
      {
          id: new Date().getMilliseconds().toString(),
          imagePath: "url..."
      },
  ];

const Upload = () => { 


    return (
    <>
       // Markup ....
    </>
    );
};

Then import them.

import { photos } from './COMPONENT_NAME';

Upvotes: 2

Related Questions