2017-02-25 8 views
0

基本的に、イベントがどこかにバインドされていて発砲していないという兆候はありません。ここではコンポーネントReact onClickイベントをReduxに正しくバインドする方法はありますか?

class AgendaPointsList extends React.Component { 
    constructor(props) { 
    super(props); 
    this.onAgendaPointClick = this.props.onAgendaPointClick.bind(this); 
    } 

    render() { 
    let items = this.props.agenda_points.map((a, i) => { 
     return <AgendaPoint key={i} agenda_point={a} index={i} onClick={this.onAgendaPointClick} /> 
    }) 

    console.log(this.props) 

    return (
     <table> 
     <tbody> 
      {items} 
     </tbody> 
     </table> 
    ); 
    } 
} 

にconsole.log(this.props)の出力です:

Object 
item_point: Object 
item_points: Array[4] 
onItemPointClick: onItemPointClick(id) 
onModalCloseClick: onModalCloseClick(id) 
store: Object 
storeSubscription: Subscription 
__proto__: Object 

ここでReduxのコンポーネントです:

子コンポーネントです
const OPEN_AGENDA_POINT = 'meeting/OPEN_AGENDA_POINT' 
const CLOSE_AGENDA_POINT = 'meeting/CLOSE_AGENDA_POINT' 

const initialState = { 
    modal_is_open: false, 
    point_id: 0, 
    point_data: {} 
} 

const openAgendaPoint = function (id) { 
    return { 
    type: OPEN_AGENDA_POINT, 
    id: id 
    } 
} 

const closeAgendaPoint = function (id) { 
    return { 
    type: CLOSE_AGENDA_POINT, 
    id: id 
    } 
} 

const agendaPointsReducer = function (state = initialState, action) { 
    switch (action.type) { 
    case OPEN_AGENDA_POINT: { 
     state.modal_is_open = true, 
     point_id = action.id 
    } 
    case CLOSE_AGENDA_POINT: { 
     state.modal_is_open = false 
    } 
    default: 
     return state 
    } 
} 


const agendaPointsUiStateProps = (state) => { 
    return { 
    agenda_point: state.point_data 
    } 
} 

const agendaPointsUiActions = (dispatch) => { 
    return { 
    onAgendaPointClick: (id) => { 
     console.log(id) 
     dispatch(openAgendaPoint(id)) 
    }, 
    onModalCloseClick: (id) => { 
     dispatch(closeAgendaPoint(id)) 
    } 
    } 
} 

const store = Redux.createStore(
    agendaPointsReducer, 
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() 
) 

// Usage: 
const AgendaPointsList = connectWithStore(
    store, 
    AgendaPointsList, 
    agendaPointsUiStateProps, 
    agendaPointsUiActions 
) 

class AgendaPoint extends React.Component { 
    render() { 
    return (
     <tr> 
     <td>{ this.props.index + 1 }</td> 
     <td>{ this.props.agenda_point.title}</td> 
     <td>6</td> 
     <td>{ this.props.agenda_point.agenda_time } min</td> 
     </tr> 
    ); 
    } 
} 

複数のバインディング方法を試しましたイベント:

onClick={this.props.onAgendaPointClick.bind(a.id, this) 
onClick={this.props.onAgendaPointClick(a.id, this).bind(this) 
onClick={() => this.props.onAgendaPointClick(a.id)) 

これはうまくいきません。

thisを使用してreac-redux connectラッパーを保管します。これはRuby on Rails Sprockets beta4上で実行されています。

これを行う正しい方法は何ですか?

+2

修正のonClick = {this.props.onItemClick.bind(これ、a.idは) ' – cubbuk

+1

これは動作するはずです'だろうバインディング: 'のonClickあなたはイベントがtriggerdされる次のコード変更に伴い = {()=> this.props.onItemClick(a.id))} ' この場合、_this_にバインドする際には使用できません。 このアプローチを使用するとどのようなエラーが発生しますか? – mahieus

+0

@cubbuk Reactのデバッグツールは、onItemClick() "'にバインドされた 'onClick = 'として認識されるようになりましたが、何も起こらず、イベントログ自体のコンソールログは何もしません – Aurimas

答えて

3

オンクリックをあなたのタグにします。

class AgendaPoint extends React.Component { render() { 
    return (
     <tr onClick={this.props.onClick}> 
     <td>{ this.props.index + 1 }</td> 
     <td>{ this.props.agenda_point.title}</td> 
     <td>6</td> 
     <td>{ this.props.agenda_point.agenda_time } min</td> 
     </tr> 
    ); } } 
1

は、あなたのITEMLISTコンストラクタでイベントを結合してみてください。あなたのITEMLISTレンダリング機能で次に

constructor(props) { 
    super(props); 
    this.onItemClick = this.onItemClick.bind(this); 
    } 

...これはonItemClick機能はITEMLIST親に定義されていることを前提としてい

let items = this.props.agenda_points.map((a, i) => { 
    return <Item key={i} agenda_point={a} index={i} onClick={this.props.onItemClick} /> 
    }) 

を、そしてあります支柱として渡される。

+0

react開発ツールは、イベントがバインドされていることを示しています(私が試した他のケースと同じように)が、実際には実際のDOMには登録されていません。何が間違っていますか?ありがとう – Aurimas

+0

ItemListレンダリング関数で、 'Item'をレンダリングしていますが、あなたの子コンポーネントは実際には' ItemPoint'と呼ばれています。おそらくこれは問題の一部ですか?編集:ちょうど@jpdelatorreが既に別の方法でこれを尋ねていることを理解しました。 – grizzthedj

+0

いいえ、以前はコード内の名前を変更しましたが、実際のコードに変更されました。これ以上のアイデアは? – Aurimas

関連する問題