React-Redux-Thunk:动作不返回调度 [英] React-Redux-Thunk: actions does not return dispatch

查看:88
本文介绍了React-Redux-Thunk:动作不返回调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Redux-thunk 中间件的 React Native.我的问题是调度函数不返回对象,甚至不控制台.

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

这是我的操作文件:

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

这是组件(我在这里不包括 const 样式以使其更短):

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!

推荐答案

您正在调用 selectMovie 操作而不是调度它.通过 bindActionToProps 方法,react-redux 为你提供了一种通过 props 调度动作的方法,所以正确的代码应该是:

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

这篇关于React-Redux-Thunk:动作不返回调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆