이것이 내 문제를 해결하는 올바른 방법인지 확실하지 않으므로 다른 방법에도 열려 있습니다.
자식 구성 요소 (이미지)를 클릭하면 부모 구성 요소의 속성에 액세스 할 수 있기를 원합니다. 지금까지 내 코드는 다음과 같습니다.
부모 구성 요소
import {Image, Grid, Row, Col} from 'react-bootstrap';
import React, { Component } from 'react';
import localForage from 'localforage';
import Activity from './activity';
testClick(e){
console.log("click handled", e.target.getAttribute('value'));
}
render() {
return (
<Grid>
<Row>
<Col sm={4} smPush={4}>
<h2 className="center-header">Explore Ibiza</h2>
</Col>
</Row>
<Row />
<Activity handleClick={(e)=>{this.testClick(e)}} description="Dining" imageSource="https://lonelyplanetimages.imgix.net/mastheads/95971965.jpg?sharp=10&vib=20&w=1200" />
<Activity description="Diving" imageSource="https://media-cdn.tripadvisor.com/media/photo-s/06/8d/1f/6a/linda-playa.jpg"/>
<Activity onClick={this.handleClick} description="Boating" imageSource="https://lvs.luxury/wp-content/uploads/2015/06/1.jpg"/>
<Row />
</Grid>
)
}
하위 구성 요소
return(
<Col sm={4}>
<p>{props.description}</p>
<Image onClick={props.handleClick} value={props.description} src={props.imageSource}className="activity-image margin-bottom-5"/>
</Col>
)
설명
현재 값을 가져올 수 있지만 description
부모 요소 의 속성도 얻으려면 어떻게해야 합니까?
나는 당신을 올바르게 이해했는지 확실하지 않지만 부모의 콜백 / 핸들러에 추가 매개 변수를 전달해야한다고 생각합니다.
아마도 다음과 같은 것이 도움이 될 것입니다.
<Image onClick={e => this.props.handleClick(e, props.description)} ...
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다