将Dispatch传递给onClick事件Redux

时间:2017-06-14 07:07:30

标签: javascript reactjs redux

我正在深入研究我的第一个react / redux应用程序,并且在将我的调度操作映射到组件中的onClick事件时遇到了一些麻烦。

我尝试了将onClick事件绑定到调度的几种变体,但我总是最终得到:

ReferenceError: onMovieClick is not defined

或者当我最终正确绑定函数时,我会收到与dispatch is not defined相关的错误。

我的目标 我正在尝试从商店功能

实现过滤器(删除)

操作/ movieActions.js

import * as actionTypes from './actionTypes'

export const createMovie = (movie) => {
  return {
    type: actionTypes.CREATE_MOVIE,
    movie
  }
};

export const deleteMovie = (id) => {
  console.log('action triggered. movie index:' + id)
  return {
    type: actionTypes.DELETE_MOVIE,
    id
  }
}

减速器/ movieReducers.js

export default (state = [], action) => {
  switch (action.type){
    case 'CREATE_MOVIE':
      return [
        ...state,
        Object.assign({}, action.movie)
      ];
    case 'DELETE_MOVIE':
      return [
        state.filter(({ id }) => id  !== action.id)
      ]
    default:
          return state;
  }
};

组件/ MovieList.js

import React from 'react'
import Slider from 'react-slick'
import { dispatch, connect } from 'react-redux'
import {Icon} from 'react-fa'
import { deleteMovie } from '../../actions/movieActions'

import 'slick-carousel/slick/slick.css'
import 'slick-carousel/slick/slick-theme.css'
import './MovieList.scss'

class MovieList extends React.Component{
  constructor(props){
    super (props)
  }

  handleClick(id) {
    dispatch(deleteMovie(id))
  }

  onMovieClick(id){
   dispatch.deleteMovie(id)
  }

  render () {
    // Settings for slick-carousel
    let settings = {
      infinite: true,
      speed: 500
    }

    return (
      <div className='col-lg-12'>
        {this.props.movies.map((b, i) =>
          <div key={i} className="col-lg-2">
            <Slider {...settings}>
              {b.images.map((b, z) =>
                <div className="img-wrapper">
                  <Icon name="trash" className="trash-icon" onClick={() =>
                    console.log(this.props.movies[i].id),
                    onMovieClick(this.props.movies[i].id)
                  }/>
                  <img className="img-responsive" key={z} src={b.base64}></img>
                </div>
              )}
            </Slider>
            <div className="text-left info">
              <h2>{b.title}</h2>
              <p>{b.genre}</p>
            </div>
          </div>
        )}
      </div>
    )
  }
}

// map state from store to props

const mapStateToProps = (state) => {
  return {
    movies: state.movies
  }
};

// Map actions to props
const mapDispatchToProps = (dispatch) => {
  return {
    onMovieClick: (id) => {
      dispatch(deleteMovie(id))
    }
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(MovieList)

如果有人有时间的话,会喜欢一些建议。

1 个答案:

答案 0 :(得分:4)

由于您通过connect传递了MovieClick,您实际上可以从MovieList组件props中调用它。首先,我将删除MovieList组件中的var coordinates = $('#coordinates').val(); var url = "https://www.google.com.sa/maps/"+ coordinates +",12.21z?hl=en" $("#map").on('click', function () { window.open(url, '_blank'); }); 方法定义,然后在onMovieClick的onclick处理程序中使用this.props.onMovieClick,如下所示:

Icon