Answer the question
In order to leave comments, you need to log in
Props, React JS arrays don't do a favor?
Hello
, there is such a component
function Main() {
const database = [
{
id: 1,
name: "MacBook",
price: 100000,
bought: false,
rating: 5,
}]
return (
<div className="main">
{database.map((product) => {
return <Maincont product={product}/>
})}
</div>
)
}
И есть еще такой компонент <Maincont/>
function Maincont(props){
return (
<div className ="card">
<div className="card-info">
<div className="card-name">{?}</div>
<div className="card-price">{?}</div>
</div>
</div>
}
Answer the question
In order to leave comments, you need to log in
If I understand the question correctly, then this problem has two solutions. The props you get in the Maincont component is an object that should have a product field (according to this post <Maincont product={product}/>
). This means that to get the name and price of the product object, you just need to refer to them: props.product.name and props.product.price. But there is a more practical way - with the help of destructuring, you can pass in props in the form of {product}. Now you can access your product object directly in the component - . But it would be even more logical in this situation to pass not the product object, but its fields!function Maincont({product}){...}
<Maincont name={product.name} price={product.price}/>
function Maincont({name, price}){...}
<Maincont {...product} />
<Maincont name={product.name} price={product.price}/>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question