[英]How I can transfer transfered props from components?
My code component is "Busket", in which I want transfer props from "Card" component:我的代码组件是“Busket”,我想在其中从“Card”组件传输道具:
import React from "react";
import s from "./Busket.css"
import styles from "../Main/MainContent.module.css"
import BusketCard from "./BusketCard";
function Busket() {
return (
<div className={styles.MainContent}>
<button>+</button>
<div className='busket ' >
<BusketCard book={require('../../../images/book1.jpg')} title="Стивен Кинг - Будет кровь" cost="1500" />
<ul>
<li className="busket__punktir_li">
<span>Итого:</span>
<div className="busket__punktir"></div>
<b>23542руб</b>
</li>
<li className="busket__punktir_li">
<span>Налог 5%:</span>
<div className="busket__punktir"></div>
<b>2542руб</b>
</li>
</ul>
</div>
</div>
);
}
export default Busket;
It's code's component "Catalog", from which I transfer props in another component "Card":它是代码的组件“Catalog”,我从中将道具转移到另一个组件“Card”中:
import React from "react";
import "./Catalog.css"
import s from "../Main/MainContent.module.css"
import 'macro-css';
import Card from "./Card.js"
function Catalog(props) {
return (
<div className={s.MainContent}>
<div className="wrapper d-flex clear justify-between m-50">
<Card img={require('../../../images/book1.jpg')} title="Стивен Кинг - Будет кровь" cost="1500" />
<Card img={require('../../../images/book2.jpg')} title="Джули Кагава - Лисья Тень" cost="1300" />
<Card img={require('../../../images/book3.jpg')} title="Джек Лондон - Белый Клык" cost="1400" />
<Card img={require('../../../images/book4.jpg')} title="Говард Лавкрфт - Хребты Безумия" cost="1100" />
</div>
<div className="wrapper d-flex clear justify-between m-50">
<Card img={require('../../../images/book5.jpg')} title="Стивен Кинг - Как писать книги" cost="2000" />
<Card img={require('../../../images/book6.jpg')} title="Рэй Брэдбери - Надвигается Беда" cost="1700" />
<Card img={require('../../../images/book7.jpg')} title="Оливер Сакс - Человек, который принял жену за шляпу" cost="1000" />
<Card img={require('../../../images/book8.jpg')} title="Оливье Гез - Исчезновение Йозефа Менгеля" cost="1800" />
</div>
</div>
);
}
export default Catalog;
The code component takes props from "Catalog" and from there I want transfer props:代码组件从“目录”获取道具,我想从那里传输道具:
import React from "react";
import "./Catalog.css"
import 'macro-css';
import Busket from "../Busket/Busket.js"
function Card(props){
const [comlete, setComplete] = React.useState(false)
const handleComplete = () => {
setComplete(!comlete)
{setComplete ? Busket.props = props : Busket = []}
}
return(
<div>
<div className="card">
<img width="200px" height="250px" src={props.img} />
<h5>{props.title}</h5>
<div className="d-flex align-center justify-between">
<div>
<p>Цена:</p>
<b>{props.cost}руб</b>
</div>
<button className="Catalog__btn" onClick={handleComplete}><img width='10px' height='10px' src={comlete ? require("../../../images/complete.png") : require("../../../images/plus.jpg") }/></button>
</div>
</div>
</div>
)
}
export default Card;
I try take props but I got (blank object):我尝试使用道具但我得到了(空白对象):
{}
[[Prototype]]
I think you should do something like this inside your handleComplete
method:我认为你应该在你的handleComplete
方法中做这样的事情:
const handleComplete = () => {
setComplete(!comlete)
{setComplete ? <Busket {...props} /> : <Busket />}
// or
// {setComplete ? <Busket {...props} /> : null}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.