最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass Dispatch to onClick Event Redux - Stack Overflow

programmeradmin1浏览0评论

I'm digging into my first react/redux application and I've been having quite a bit of trouble mapping my dispatch actions to onClick events in my ponents.

I've tried a couple of variations of trying to bind the onClick Event to the dispatch, but I always end up with either :

ReferenceError: onMovieClick is not defined

or alternatively when I do end up binding a function correctly I'll get an error related to dispatch is not defined.

My Goal I'm trying to implement a filter(delete) from store function

actions/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
  }
}

reducers/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;
  }
};

ponents/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)

Would love some advice if anyone has a moment.

I'm digging into my first react/redux application and I've been having quite a bit of trouble mapping my dispatch actions to onClick events in my ponents.

I've tried a couple of variations of trying to bind the onClick Event to the dispatch, but I always end up with either :

ReferenceError: onMovieClick is not defined

or alternatively when I do end up binding a function correctly I'll get an error related to dispatch is not defined.

My Goal I'm trying to implement a filter(delete) from store function

actions/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
  }
}

reducers/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;
  }
};

ponents/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)

Would love some advice if anyone has a moment.

Share Improve this question asked Jun 14, 2017 at 7:07 Tinus WagnerTinus Wagner 9271 gold badge7 silver badges15 bronze badges 2
  • You have duplicate definition of onMovieClick, one in props and one in the method of MovieList – Mμ. Commented Jun 14, 2017 at 7:10
  • FYI I dont believe this will work if you "Open in new tab" – Christo Commented Sep 10, 2018 at 16:16
Add a ment  | 

1 Answer 1

Reset to default 4

Since you are passing onMovieClick through connect, you can actually invoke it from the MovieList ponent props. First, I would remove the onMovieClick method definition in your MovieList ponent and then use this.props.onMovieClick in the onclick handler of Icon like so:

<Icon name="trash" className="trash-icon" onClick={() =>
  console.log(this.props.movies[i].id),
  this.props.onMovieClick(this.props.movies[i].id)
}/>

发布评论

评论列表(0)

  1. 暂无评论