1

I have an application in React where I'm trying to read in data from a JSON file. The JSON file is in the following format:

[
    {
        "name": "Max",
        "age": "21",
    },

    {
        "name": "Sam",
        "age": "18",
    }

    ........
]

I have successfully read in the right data and displayed it on my screen, like this:

function foo(){
    const styling = css`
        font-size: 30px;
    `;

    return(
        <div>
            {people.map((person, i) => <Format key={i} {...person} css={styling}/>)}
        </div>
    );
}

Although all the information correctly displays on the screen, the styling is not getting applied to each person. How could I change this?

EDIT

Format component:

function Format({name, age}){
   return (
      <div>
         <h1>{name}</h1>
         <h2>{age}</h2>
      </div>
   );
}
8
  • 1
    How your Format component looks like ? Commented Feb 17, 2020 at 10:04
  • Updated. Can I just style it there? Commented Feb 17, 2020 at 10:07
  • You are not using the css prop in Format component. Commented Feb 17, 2020 at 10:10
  • You using any library for CSS ? i don't think there's any prop named css by default and also you're not using the passed prop anywhere Commented Feb 17, 2020 at 10:10
  • 1
    Actually, I just figured it out!! Commented Feb 17, 2020 at 10:12

1 Answer 1

4
function Format({name, age, css}){
   return (
      <div css={css}>
         <h1>{name}</h1>
         <h2>{age}</h2>
      </div>
   );
}

you passed styled to your component but you didnt use them in your child component

and plus using idx as key is not the best practice. following article explains why. https://reactjs.org/docs/lists-and-keys.html

if name is unique you can pass each items name to mapped children.

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

Comments

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.