2016-12-18 7 views
0

私は以下のコンテナコンポーネントをReduxストアに接続しています。私は、LayerListプレゼンテーションコンポーネントに6つのアクションを渡したいと思います。ここで大量のReduxアクションをコンテナからプレゼンテーション・コンポーネントに渡すにはどうすればよいですか?

LayerListContainer.jsx

import React from 'react'; 
import { connect } from 'react-redux' 
import LayerList from '../components/LayerList' 

// import actions 
import * as LayerActions from '../actions/index' 

export const LayerListContainer = ({ layers }) => (
    <div> 
    <LayerList layers={layers} /* I want to pass actions to this component/> 
    </div> 
) 

const mapStateToProps = state => ({ 
    layers: state.layers 
}) 

// Use default export for the connected component (for app) 
export default connect(mapStateToProps, LayerActions)(LayerListContainer) 

は私が渡したいアクションは、次のとおりです。

アクション/ index.js

export const addLayer = (name) => ({ 
    type: ADD_LAYER, 
    id: nextLayerId++, 
    name, 
}) 

export const removeLayer = (id) => ({ 
    type: REMOVE_LAYER, 
    id 
}) 

export const toggleDragLayer = (id) => ({ 
    type: TOGGLE_DRAG_LAYER, 
    id 
}) 

export const moveLayerIndex = (id, destinationIndex) => ({ 
    type: MOVE_LAYER_INDEX, 
    id, 
    destinationIndex 
}) 

export const updateLayerColour = (id, colour) => ({ 
    type: UPDATE_LAYER_COLOUR, 
    id, 
    colour 
}) 

export const toggleLayerVisibility = (id) => ({ 
    type: TOGGLE_LAYER_VISIBILITY, 
    id 
}) 

おそらく、あなたはこのことを考慮していません大量の行動になる。そうでない場合は、コンテナコンポーネントから多くのアクションを渡す際の参考になるベストプラクティスを知りたいと思っています。

答えて

0

あなたはこの

export const LayerListContainer = props => (
    <div> 
    <LayerList {...props} /> 
    </div> 
); 

よう...スプレッド演算子の構文を使用してプレゼンテーションにあなたのコンテナのすべてのプロパティを渡すことができますまたはあなただけのアクションを渡したい場合は、bindActionCreators

import { bindActionCreators } from 'redux' 

// ... 

export const LayerListContainer = ({ layers, actions }) => (
    <div> 
    <LayerList layers={layers} actions={actions} /> 
    </div> 
); 

// .. 

const mapDispatchToProps = dispatch => ({ 
    actions: bindActionCreators(LayerActions, dispatch) 
}) 

export default connect(mapStateToProps, mapDispatchToProps)(LayerListContainer) 
+0

を使用する必要がありますそれはアクションクリエイターと一緒に派遣されますか? – therewillbecode

+0

問題のアクションクリエイターに関するもので、他の種類のものではないので、答えの最初のセクションの目的は何ですか? – therewillbecode

+0

最初のコードブロックは機能しません。ステートレス反応コンポーネントでは、これは単なる関数なので、 'this'を参照することはできません。 'this'を使用するには、コンポーネントにクラスベースの構文を使用する必要があります。 – therewillbecode

関連する問題