2016-06-06 1 views
0

몇 시간 동안 redux로 작업하기 이제 나를 차단하는 구성이 있습니다.reactjs Redux, 첫 번째 작업이 이미 전송되는 동안 redux onComponentDidmount에서 데이터를로드하는 방법

componentDidMount() { 
    const { dispatch } = this.props; 
    dispatch(fetchPosts('trendings')); 
    (this.props.message) ? dispatch(removeMessage('')) : '' ; 
    } 

dispatch(fetchPosts('trendings')); 내부 구체적인 배열과 props.posts을 채우기 :

여기 내 ComponentDidMount입니다. 이 props.posts가 채워지면 다른 디스패치를 ​​피드에 사용해야하지만 ComponentDidMount에있는 모든 액션을 피드해야합니다.

여기 내 문제가 있습니다. 두 번째 발송은 첫 번째 발송이 진행되는 동안 전화가 걸립니다. 내가 이런 식으로 수행해야합니다

var outputList = this.props.posts.items['output']; 
    const { dispatch } = this.props; 

    if (typeof outputList !== "undefined") { 
     outputList.map((item, i) => { 
     dispatch(newOutput(item.MarkerName)); 
     }); 

내가 componentDidUpdate이 코드를 넣어 노력을하지만 문제는이 기능이 자동으로 내가 원하지 않는, 액션이 발생할 때마다 트리거 것입니다.

방법이 있습니까?

감사합니다.

답변

1

당신은 비틀기와 함께, componentWillReceiveProps에서 작업을 수행 할 수 있습니다 게시물이 항목 배열에 빈 배열되었을 때

componentWillReceiveProps(nextProps) { 
    if (this.props.posts.length === 0 && nextProps.posts.length > 0) { 
    /* Do stuff here */ 
    } 
} 

이 방법은 한 번만 코드를 실행할 수 있습니다. 재 렌더링이 필요한 경우 shouldComponentUpdate에서 수행해야합니다.

+0

굉장! 감사합니다 Kujira! –

관련 문제