2017-05-20 3 views
0

別の.jsファイルに関数を記述してコードを分割しています。this.props.dispatchを関数に代入する方法は?

問題は、新しい.jsファイルでクラスを使用していないため、私のredux接続関数へのアクセス権がないことです。

物事をクリアするには:

これは私が私のユーザーを使用するためには

@connect((store) => { 
    return { 
    bar_change:store.bar_change 
    }; 
}) 
export default class Bardoctor extends React.Component { 
    constructor(props) { 
    super(props); 

    } 
    barChange(arg0){ 
    //function code... 
    this.props.dispatch(user.changeBarNum(arg0,"bar_change_doctor")); 
    this.props.dispatch(user.sendBarDataDoctor("doctor_bar_send",temp_arr, flag_it, arg0)); 
    } 

ファイル別のJSに移動したい私のクラスの内部機能(barChange)です。私はthis.props.dispatchを持っている必要があります。 barChangeファンクションを別の.jsファイルにパックし、そのファンクションをエクスポートしてクラスに呼び出すと、this.props.dispatchをどのように使用しますか?例えば

import * as user from "../actions/asyncCAll.js" 
export function barChange(arg0, prop_arr, prop_next, string_change, string_send){ 
    //function code... 
    dispatch(user.changeBarNum(arg0,string_change)); 
    dispatch(user.sendBarDataPatient(string_send, temp_arr, flag_it, arg0)); 
} 
+0

'dispatch'メソッドを渡します。 –

答えて

2

あなたはあなたの新しいJSファイルにstoreをインポートして、直接店舗のdispatch methodを使用することができます。

import store from 'path/to/your/store/file' 

function barChange(arg0) { 
    //function code... 
    store.dispatch(user.changeBarNum(arg0, "bar_change_doctor")); 
    store.dispatch(
    user.sendBarDataDoctor("doctor_bar_send", temp_arr, flag_it, arg0) 
); 
} 

あなたがcreateStoreメソッドを使用して別のファイルにあなたのストアを作成していない場合、私は次のようにそれをすることをお勧めします。

const store = createStore(reducers, //...); 

export default store; 

警告:あなたは、サーバー側のレンダリングとそれを使用する場合は、このアプローチは、合併症を持っているかもしれません。

+2

ちょうど警告:isomorphic/universal appを開発している場合は、ストアをシングルトンとしてインポートして問題につながります。 –

+0

私はxdではないことを願っています。 –

+0

@LucaFabbriは、それを指摘してくれてありがとう。私は答えを更新した –

関連する問題