React-Redux-Thunk: actions does not return dispatch

笑着哭i 提交于 2020-01-05 07:05:49

问题


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:


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)); }}>


来源:https://stackoverflow.com/questions/47698595/react-redux-thunk-actions-does-not-return-dispatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!