2017-02-16 2 views
0

React/Redux를 처음 사용하고 프로그래밍하는 것을 처음으로 접했습니다. 아래는 3 파일 사이에 내 코드입니다. 내 React-Router를 감싸는 공급자에게 저장소를 전달합니다. 문제는 1)에서, getTweets을 실행하고 작업을 제대로 가져올 수 있습니다. testing()이 문제가 없기 때문입니다. 그러나 fetchAllTweets의 디버거는 결코 동작하지 않습니다. 재향 군인 여러분, 내 문제가 무엇인지 알려주실 수 있습니까?mapDispatchToProps 메소드가 액션을 디스패치하는 방법

1) 관련 컨테이너 코드 :

import {connect} from 'react-redux'; 
    import Feed from './feed'; 
    import {fetchAllTweets, testing} from '../../actions/tweet_actions'; 
    import thunk from 'redux-thunk'; 

    const mapDispatchToProps = (dispatch) => ({ 
     getTweets:() => { 
      testing(); 
      return dispatch(fetchAllTweets); 
     } 
    }); 

    const FeedContainer = connect(mapStateToProps, mapDispatchToProps)(Feed); 

    export default FeedContainer; 

2) 관련 활동 코드

import * as APIUtil from '../util/tweet_api_util'; 
import Constants from '../constants/constants'; 
import thunk from 'redux-thunk'; 

export const fetchAllTweets =() => dispatch => { 
    debugger; 
    console.log('fetch all tweets action'); 
    APIUtil.fetchAllTweets() 
     .then(tweets => dispatch(receiveTweets(tweets))), 
     err => dispatch(receiveErrors(err.responseJSON)) 
}; 

export const testing =() => { 
    debugger; 
    console.log("worked"); 
} 

3) 스토어 당신은 bindActionCreators을 시도하고 그것을 사용할 수도 있습니다

import { createStore, applyMiddleware } from 'redux'; 
import RootReducer from '../reducers/root_reducer'; 
import thunk from 'redux-thunk'; 

const configureStore = (preloadedState = {}) => (
    createStore(
    RootReducer, 
    preloadedState, 
    applyMiddleware(thunk) 
) 
) 

export default configureStore; 

답변

1

당신은 dispatch 인수가 아닌 행동 작성자 기능 자체로 fetchAllTweets 행동 작성자에 의해 반환 된 값을 전달한다로 부른다.

사용이 :

대신의
return dispatch(fetchAllTweets()); 

: 생산 코드이 좋은 연습이

return dispatch(fetchAllTweets); 
0

코드 귀하의 컨테이너는 다음과 같습니다 :

그런 다음 구성 요소에 불과 this.props.actions.fetchAllTweets()this.props.actions.test()

+0

감사입니까? – stckoverflowaccnt12

+0

좋은 연습입니다. 이렇게하면 컨테이너 내부에 redux 로직을 유지하는 데 도움이됩니다. 즉, 구성 요소가 더 깨끗해지며 소품의 액션 만 호출하면됩니다. –

관련 문제