0

I'm making a card game. I have this json db:

{
  "characters": [   
    {    
      "name": "Character1",
      "punch":12,
      "kick":12,
      "special":15,
      "image":"character1"

    },
    {    
      "name": "Character2",
      "punch":13,
      "kick":11,
      "special":15,
      "image":"character2"  
    },         

    {   
      "name": "Character3",
      "punch":20,
      "kick":19,
      "special":10,
      "image":"character3"   
    }
  ]
}

I have this parent component that fetches the json data to pass for a child component. The data is fetched successfully.

import React, { Component } from 'react'
import Player from './Player'

var data = require('./db.json');

class Stage extends Component {
  constructor(props) {
    super(props)
    this.state = {
      characters: []
    }
  }

  componentDidMount() {
    this.setState({
      characters: data.characters
    })  
  }

  render() {
    return (

      <div className="stage">
      {console.log(this.state.characters)}
        <Player data={this.state.characters}/>
        <Player data={this.state.characters}/>
      </div>
    )
  }
}


export default Stage

Now I want to render each card for each character inside this component in a dynamic <li>. For example, I have 3 characters so I want to render 3 cards. Each one that matches for a character. How can I do it? I want to pass the properties for the <Card> component to

import React from 'react'
import Card from './Card'   

const Player = (props) => { 
  return (
    <div className="player">     
      <Card/>  
    </div>
  )
}


export default Player 
4
  • 1
    ok, so what is the problem? Commented Mar 5, 2019 at 4:27
  • I don't know how to render a dynamic html list each one with a character into the card Commented Mar 5, 2019 at 4:28
  • see the second component Commented Mar 5, 2019 at 4:29
  • as a good practice and SO etiquette, try to mention what you have tried before posting, draft your question under requirement and problem statement headings. Just a suggestion :) Commented Mar 5, 2019 at 6:32

2 Answers 2

2

For this you want to use .map(). Basically you take an array, and return a set of react components from that array. For example:

const Player = ({ data }) => { 
  return (
    <div className="player">
      {data.map((character, i) => (     
        <Card key={i} name{character.name}/>  
      ))}
    </div>
  )
}

Here I have passed the name key down into each card, but you can pass whatever you want (including the entire character object if that makes sense).

Also, I would rename data to characters as this makes more sense. And I would set a default to the prop to be an empty array.

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

6 Comments

But I want to do it in the cards, not in the players
You might need to clarify what you are trying to achieve then. Your json data is an array of what looks like characters. What do the cards represent? Could you provide say your ideal component tree output or something? Either way, what i've put as my answer should be what you need conceptually.
each card represent each character
ok updated. As you can see, the answer is still conceptually the same.
please don't forget key=[i] for card child.
|
1

Simply use .map() function to loop every item of an array.

.map()

  {data.map((item, i) => (     
    <Card key={i} name={item.name}/>  
  ))}

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.