React-Redux-Thunk: actions does not return dispatc

2019-07-31 08:45发布

I am using React Native with Redux-thunk middleware. My problem is dispatch function does not return the object and does not even console.

Here is my actions file:

function movieSelc(movie) {
    return {
            type: types.MOVIE_SELECT,
            selectedMovie: movie
        };
}

export function selectMovie(m) {
    console.log("this console works!")
  return (dispatch) => {
    console.log("this console never works")
    dispatch(movieSelc(m));
  };
}

This is the component(i am not including here const styles to make it a bit shorter):

import React, { Component } from 'react';
import { Text, View, TouchableOpacity, Image, } from 'react-native';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import * as actions from './../actions';

class ListItem extends Component {
    render() {
        const { movie } = this.props;
        return (

            <View>
                    {
                      movie && movie.map((item, index) =>
                        <View key={index} style={styles.containerStyle}>
                             <Image
                                 source={{ uri: `https://image.tmdb.org/t/p/w342${item.backdrop_path}` }}
                                 style={styles.imgStyle}
                             />

                            <TouchableOpacity
                                 style={{ backgroundColor: 'gray' }}
                                 onPress={() => { actions.selectMovie(item); }}
                            >
                                  <Text style={styles.headStyle}>{item.title}</Text>
                                  <Text>{item.overview}</Text>
                                  <Text style={styles.rateStyle}>
                                    Release Day:{item.release_date}
                                  </Text>
                                  <Text style={styles.rateStyle}>Rating: {item.vote_average}</Text>
                            </TouchableOpacity>
                        </View>)
                    }
            </View>

        );
    }
}
ListItem.propTypes = {
    actions: PropTypes.object.isRequired,
    movies: PropTypes.object.isRequired,
};


function mapStateToProps(state) {
    const { movies } = state;
    return {
        movies,
    };
}

function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators(actions, dispatch)
    };
}

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(ListItem);

Please let me know if I need to provide more information. Thank you!

1条回答
2楼-- · 2019-07-31 09:35

You are calling selectMovie action instead of dispatching it. With the bindActionToProps method, react-redux provide you a way to dispatch actions via props, so the correct code should be:

<TouchableOpacity 
style={{ backgroundColor: 'gray' }}
onPress={() => { this.props.actions.selectMovie(item)); }}>
查看更多
登录 后发表回答