2016-11-28 2 views
3

그래서 저는 ApiClient 도우미가있는 반감기 보일러 플레이트를 사용하고 있습니다. 그것은 다음과 같습니다 :이 작업을 수행 할 때, 나는를 얻을비 구성 요소 클래스를 redux 저장소에 연결할 수 있습니까?

@connect(state => ({ jwt: state.auth.jwt })) 
export default class ApiClient { 
    ... 

을하지만 :

export default class ApiClient { 
    constructor(req) { 
    /* eslint-disable no-return-assign */ 
    methods.forEach((method) => 
     this[method] = (path, withCredentials, { params, data } = {}) => new Promise((resolve, reject) => { 

     const request = superagent[method](formatUrl(path)) 

     if (withCredentials) { 
      console.log('first of all, its true') 
      console.log(this) 
     } 

     if (params) { 
      request.query(params) 
     } 

     if (__SERVER__ && req.get('cookie')) { 
      request.set('cookie', req.get('cookie')) 
     } 

     if (data) { 
      request.send(data) 
     } 

     request.end((err, { body } = {}) => { 

      return err ? reject(body || err) : resolve(body) 

     }) 
     })) 
    /* eslint-enable no-return-assign */ 
    } 
    /* 
    * There's a V8 bug where, when using Babel, exporting classes with only 
    * constructors sometimes fails. Until it's patched, this is a solution to 
    * "ApiClient is not defined" from issue #14. 
    * https://github.com/erikras/react-redux-universal-hot-example/issues/14 
    * 
    * Relevant Babel bug (but they claim it's V8): https://phabricator.babeljs.io/T2455 
    * 
    * Remove it at your own risk. 
    */ 
    empty() {} 
} 

은 내가 보호 엔드 포인트로 헤더를 붙일 수 있도록과 같이, 내 인증이를 연결하려면 오류 : Cannot read property 'store' of undefined. 무슨 일 이니? 일반 클래스를 redux 상점에 연결할 수없는 이유는 무엇입니까? 업데이트

: 여기 ApiClient 도우미 사용하여 내 로그인 기능이다 :

export function loginAndGetFullInto(email, password) { 
    return dispatch => { 
    return dispatch(login(email, password)) 
    .then(() => { 
     return dispatch(loadUserWithAuth()) 
    }) 
    } 
} 

내가 loadUserWithAuth 기능으로, 가게, 또는 JWT을 통과 중 어떤 방법이 필요를 ...

+0

*'connect()': ** React 구성 요소 **를 Redux 저장소 *에 연결합니다. 단순히 생성자/속성을 통해 클래스에 저장소에 대한 참조를 전달할 수 없습니까? – CodingIntrigue

+0

@ 코딩 시작 방법? –

+0

다음과 같이 :'var apiClient = new ApiClient (createStore()); .... 생성자 (저장소) {store.getState(); }' – CodingIntrigue

답변

8

connect 함수는 React 구성 요소 이외의 함수와 함께 작동하지 않습니다. 상점 인스턴스를 클래스에 전달하고 store.dispatch, store.getStatestore.subscribe을 직접 호출 할 수 있습니다.

구독하는 경우 구독 취소 기능도 있어야합니다. 그렇지 않으면 저장소가 클래스 인스턴스에 대한 참조를 영원히 보유하여 메모리 누수가 발생합니다.

+0

상점을 직접 상수라고 부를 수 있다는 것을 몰랐습니다. 내 플럭스 라우터를 다시 렌더링하지 않고도 앱 구성 요소와 기존의 연결을 할 수 없었기 때문에 내 앱 구성 요소에 이상적입니다. – ArcadeRenegade

관련 문제