Here are the files in question:
card-list.styles.css: src -> component -> card-list -> card-list.styles.css
.card-list {
width: 85vw;
margin: 0 auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-gap: 20px;
}
card-list.component.jsx: src -> component -> card-list -> card-list.component.jsx
import React from 'react';
import './card-list.styles.css';
import {Card} from '../card/card.component';
export const CardList = (props) => (
<div className="card-list">
{props.monsters.map(monster => (
<Card key={monster.id} monster={monster}/>
))}
</div>
);
card.component.jsx: src -> component -> card -> card.component.jsx
import React from 'react';
export const Card = props => (
<div>
{console.log(props.key)}
<h1>{props.monster.name}</h1>
</div>
);
App.js: src -> App.js
import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';
import {CardList} from './component/card-list/card-list.component'
class App extends Component {
constructor() {
super();
this.state = {
monsters: []
};
}
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(users => this.setState({monsters : users}));
}
render () {
return (
<div className="App">
<CardList monsters={this.state.monsters}/>
</div>
);
}
}
export default App;
Super new to React and Javascript, my question: In card-list.component.jsx, if my terminology is right, I'm passing key
and monster
as props
to Card
component. While the monster
prop is accessible, the key
prop is undefined. Why is the key
prop coming in as undefined?
What is the effort I've put in to understand this: I've searched for generic questions asking why props.name
is undefined, and there are multiple answers about setState
happening asynchronously and componentWillMount
returns a promise and is asynchronous.
Why is this not duplicate question if the above answer is right?: Why is ONE of my props monster
available for use? but key
alone is undefined
so not available for use? Why does the asynchronous activity affect one of the props but not the other?