React로 시작한 것만으로도 사용자 인터페이스를 만드는 것이 좋을 것 같지만 구성 요소 간 통신에 문제가 있습니다. 내가해야합니까React 구성 요소 간 통신
은 매우 단순 버튼을 나타내는 요소를 반응 :
import React from 'react';
export default class OfficeUIButton extends React.Component {
constructor(props) {
super(props);
this.state = {
active: false,
};
// Bind the event handlers.
this.onMouseDownEventHandler = this.onMouseDownEventHandler.bind(this);
this.onMouseUpEventHandler = this.onMouseUpEventHandler.bind(this);
}
onMouseDownEventHandler() {
this.setState({
active: true
});
}
onMouseUpEventHandler() {
this.setState({
active: false
});
}
render() {
return <div className={"officeui-button" + (this.state.active ? ' active': '')} onMouseDown={this.onMouseDownEventHandler}
onMouseUp={this.onMouseUpEventHandler}>
<span className="officeui-button-label">{this.props.text}</span>
</div>
}
}
이 구성 요소는 active
라는 이름의 상태 속성이 있습니다. 상태에 따라 버튼에 추가 클래스가 추가됩니다.
내 페이지에 OfficeUIButton
개 중 2 개가 있다고 가정 해 봅시다. 어떻게 첫 번째 버튼을 클릭하여 두 번째 버튼을 활성화 할 수 있습니까?
이것은 모범적 인 예일뿐입니다. 예를 들어 모달 팝업을 사용하거나 특정 작업을 기반으로 버튼을 사용하지 않도록 설정해야합니다.
도움 주셔서 감사합니다.
[상태를 들어 올리십시오] (https://facebook.github.io/react/docs/lifting-state-up.html). 여러 구성 요소가 어떻게 든 서로 의존하는 경우 국가는 공통 조상 중 하나가 소유해야합니다. –
가능한 중복 : [ReactJS 두 구성 요소 통신] (https://stackoverflow.com/questions/21285923/reactjs-two-components-communicating) – oklas