0

This might be a noob react question (still learning), I have this constant:

const data = {
        first_name:   "John",
        last_name:    "smith",
        employee:     "Google"  
    };

That I pass to another function. This works but I want to move it to own file to make it more generic. If I move it to person-data.js:

import React, { useEffect } from "react";

const PersonData = props => {
    const data = {
        first_name:   "John",
        last_name:    "smith",
        employee:     "Google" 
    };
    return (
        {data}
  );
};

export default PersonData;

and in my main component:

import person from '../person-data'
.....
// temp to test
const data = {
        first_name:   "John",
        last_name:    "smith",
        employee:     "Google"  
    };
.....
somefunction(person) // doesnt work
somefunction(data)   // works


it no longer works, the reason I want to move it is so I can use props to pass the info dynamically. Is this even a good way to do this?

2
  • I have provided you with complete CodeSandBox Link also. codesandbox.io/s/upbeat-cookies-hdeiw?file=/src/PersonData.js . My answer is how we do this in React :) Check it once Commented Dec 23, 2020 at 20:39
  • I think I got your query now. When you said "but I want to move it to own file to make it more generic. If I move it to person-data.js". Generic means a seperate file with data only, not a React Component Bro :) Anyways, So, what you actually are doing is creating a seperate object to hold the data only. Is this a good Idea. I don't think so. Components are to hold state only to render that in JSX. Any way ;) That's is state management issue I guess :) Commented Dec 23, 2020 at 20:52

4 Answers 4

1

It doesn't work because you're returning an object using destructuring syntax, without spreading the data object into the new object.

Try:

const PersonData = props => {
    const data = {
        first_name:   "John",
        last_name:    "smith",
        employee:     "Google" 
    };
    return {...data} // or just: return data
};

export default PersonData;
import person from '../person-data'
.....
// temp to test
const data = {
        first_name:   "John",
        last_name:    "smith",
        employee:     "Google"  
    };
.....
somefunction(person())

They way you have it in your code above, you are returning an object of the form:

{
   data: {
      first_name: 'John',
      last_name: 'smith',
      ...
   }
}

Thus you would have to lookup PersonData(props).data.first_name to get your first name, for example.

Sign up to request clarification or add additional context in comments.

6 Comments

this didnt work, I passed it like this after import somefunction(person) but it is not working the same as somefunction(data)
Ah, that's because you're exporting a function (see edit). That is, person is a function, not an object. It returns an object when it is called. You're importing it and passing it as if it were still an object (like data)
this returned the correct data somefunction(person()). To better understand this, why it wasnt working with person if it returns the object similar to data?
Excellent. Does that answer your question, then? Or is there more I can help out with?
const PersonData = props => {...} stores an arrow function in the variable PersonData. Then export default PersonData; exports that variable, and again the value of that variable is the arrow function. Consequently, when you import it with import person from '../person-data' you're storing that arrow function in the local variable person. So, if you were to do: console.log(typeof person) you'd see: "function". For that reason, you want to call the function, since it returns the data object. So console.log(person()) produces: "{first_name, ...}". Does that make sense?
|
0
const personData = {
  first_name: "John",
  last_name: "smith",
  employee: "Google" 
};
export default personData;

You can export the const. It doesn't need to be within a component. If you want to change the values, you could make a function. E.g.

function getPersonData(firstName) {
  return {
    first_name: firstName,
    last_name: "smith",
    employee: "Google" 
  };
}

Comments

0

If I understood what you mean, what you have to do is to create a file personData.js with this content:

const personData = {
    first_name: "John",
    last_name: "Smith",
    employee: "Google"  
};

export default personData;

And then import the previous file where you need it

import personData from './personData.js';

and you can access to personData.first_name, personData.last_name and personData.employee where you need.

2 Comments

But, why would this javascript file have a .json extension?
it was a typo, fixed it
0

You need to export this file from there and import in your component.

CODESANDBOX WORKING DEMO: https://codesandbox.io/s/upbeat-cookies-hdeiw?file=/src/PersonData.js

person-data.js file:

const data = {
    first_name:   "John",
    last_name:    "smith",
    employee:     "Google"  
};

export default data;

Now in your Component import it.

import React, { useState, useEffect } from "react";

import personData from "./person-data.js"; // Provide your relative path

const PersonData = (props) => {
  const [data, setData] = useState(personData);
  return (
    <>
      <div>
        <p>{data.first_name}</p>
        <p>{data.last_name}</p>
        <p>{data.employee}</p>
      </div>
    </>
  );
};

export default PersonData;

5 Comments

yes, I already imported it in my other component
Then you can store it in the state. Let me update my answer further :) Wait bro
I have provided complete code in my update. If you have any questions, feel free to ask :)
Consider not returning inside a return.
@evolutionxbox: Oh dear! That was a typo in the code. But codesandbox.io/s/upbeat-cookies-hdeiw?file=/src/… is my codesandbox solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.