React & Flux를 사용하여 구성 요소, 액션 및 상점이 있습니다.[React] [flux] [EventEmitter]이 참조는 구성 요소에서 참조로 변경됩니다.
저장소에서 이벤트를 구성 요소의 메서드로 전달할 때 내 구성 요소의 "this"참조가 "저장소"참조로 변경됩니다.
혹시 이런 일이 일어나는 이유를 아십니까?
가게는() 메소드 getAllVotes에서, 여기에 이벤트를 전달한다 : https://huit.re/voteStoreRow41
import { EventEmitter } from 'events';
import dispatcher from '../dispatcher/dispatcher.jsx';
import request from 'superagent';
import nocache from 'superagent-no-cache';
const BASE_URL = "http://odu.priv/ws";
class VoteStore extends EventEmitter {
constructor(){
super()
this.votes = [];
this.voteComponent = {};
dispatcher.register(this.handleActions.bind(this));
}
getAll(){
return this.votes;
}
setVotes(listVote){
this.votes.push(listVote);
this.emit('change');
}
getAllVotes(){
this.emit('change');
}
그리고 구성 요소는 여기에이 이벤트를 처리 : https://huit.re/votesL33 어디 updateVote에서 voteStore의 심판을 "이"변화() 메소드를 .
import React from 'react';
import { Vote } from '../vote/vote.jsx';
import voteStore from '../../stores/voteStore.jsx';
import { getAllVote } from '../../actions/voteActions.jsx';
class Votes extends React.Component {
constructor(props) {
super(props);
console.log('const -> ', voteStore);
this.state = {
votes : voteStore.getAll()
};
}
componentWillMount() {
voteStore.on('change', this.updateVote);
getAllVote();
console.log("Votes ", this.state.votes);
}
/*componentWillUnmount() {
voteStore.removeListener('change',this.updateVote);
}*/
updateVote(){
console.log('this -> ',this);
console.log('voteStore -> ',voteStore)
this.setState({
votes : voteStore.getAll()
});
console.log('this.state',this.voteComponent.votes);
}
은 내가 그냥 이해하지 못하는 것은 왜 더 이상 빨리 방법 가게에서 호출되는 "()는 getAllVotes"로 내 "이"심판하지 않을 경우 내 "투표"예. 이로 인해 아래 행의 "this.state"가 정의되지 않습니다.
미리 감사드립니다.
내일 변경되거나 사라질 수있는 제 3 자 사이트가 아니라 여기에서 문제의 최소한의 표현을 게시해야합니다. – Rob
두 링크가 같은 행을 가리 킵니다. 이상한. 죄송합니다. –
@Rob을 업데이트하십시오. 나는 내용을 업데이트했다. 작은 링크가 개인적인 Git 저장소를 가리키고 있습니다. 의심의 여지가 내일은 사라지지 않을 것입니다 ...;) 나는 형식이 올바로 지정된 코드를 직접 보여주기 쉽지만 자세한 내용을 보려면 복제 할 수 있습니다. – Olivier