开发者问题收集

在组件 Material UI ReactJS 中显示数据列表

2019-11-09
794

我正在尝试使用 redux 和 Material UI 显示影片列表。

FilmsService :

export const FilmServices = {
    getAllFilms,

};


function getAllFilms(){
    return axios.get(config.baseUrl).then((response)=>{
        return response;
    }).catch((err)=>{
        console.log(err);
    })
}

FilmAction :

export function getAllFilms() {
    return dispatch => {
        FilmServices.getAllFilms()
            .then((response) => {
                if (response) {
                    dispatch(GET_FILMS(response));
                }
            })
    }
}

FilmReducer :

   const initialState = { films: [] }
export function filmreducer(state = initialState, action) {
    switch (action.type) {
        case 'GET_FILMS':
            console.log(action);
            return{
                ...state,
                films : action.payload.data

            };
        case 'GET_FILMS_BY_NAME':
            return{
                ...state,
                films : action.payload.data

            };
        default:
            return state
    }
}

这是我的组件:

import React , { Component }from 'react';
import PropTypes from 'prop-types';
import {makeStyles, withStyles} from '@material-ui/core/styles';
import {getAllFilms} from "../store/actions/FilmActions";
import Button from '@material-ui/core/Button';
import Card from "@material-ui/core/Card/Card";
import CardActionArea from "@material-ui/core/CardActionArea/CardActionArea";
import CardMedia from "@material-ui/core/CardMedia/CardMedia";
import CardContent from "@material-ui/core/CardContent/CardContent";
import Typography from "@material-ui/core/Typography/Typography";
import CardActions from "@material-ui/core/CardActions/CardActions";
import connect from "react-redux/es/connect/connect";
import { withRouter } from 'react-router-dom';

const useStyles = makeStyles({
    card: {
        maxWidth: 345,
    },
});

class FilmsCard extends Component {


    componentDidMount () {
        const {dispatch} = this.props;
        dispatch(getAllFilms());
    }

    constructor(props) {
        super(props);
        this.state = {
            change: ''
        };
    }

    render() {
        const films = this.props.films || [];
        return (
            <Card>
                {films.map(film => {
                    return(
                    <CardActionArea>
                        <CardMedia
                            component="img"
                            alt="Contemplative Reptile"
                            height="140"
                            image="/static/images/cards/contemplative-reptile.jpg"
                            title="Contemplative Reptile"
                        />
                        <CardContent>
                            <Typography gutterBottom variant="h5" component="h2">
                                {film.show ? film.show.name : film.name}
                            </Typography>
                            <Typography variant="body2" color="textSecondary" component="p">
                                {film.show ? film.show.name : film.name}
                            </Typography>
                        </CardContent>
                    </CardActionArea>
                        )})}
                <CardActions>
                    <Button size="small" color="primary">
                        Share
                    </Button>
                    <Button size="small" color="primary">
                        Learn More
                    </Button>
                </CardActions>
            </Card>
        );
    }
}

FilmsCard.propTypes = {
    classes: PropTypes.object.isRequired,
};

const mapStateToProps = (state) => {
    return {
        films : state.filmreducer
    };
}
const connectedVendorPage = withRouter(connect(mapStateToProps, null, null, {
    pure: false
})(withStyles(useStyles)(FilmsCard)));
export { connectedVendorPage as FilmsCard };

运行时出现此错误:

TypeError: Cannot read property 'length' of undefined

我认为 Material UI 存在问题,因为当我在简单组件(没有 Material UI)中尝试此代码时,没有出现错误。

有人可以帮帮我吗?谢谢

3个回答

@muhammad awais 我像这样更改了我的渲染(),并且它起作用了:

971113192

}

Piikaa
2019-11-10

此外,通过更改这部分代码对我有用:

 const useStyles = makeStyles({
        card: {
            maxWidth: 345,
        },
    });

更改为:

 const styles = theme => ({
        card: {
            maxWidth: 345,
        },
        media: {
            height: 50,
        },
    });
Piikaa
2019-11-10

在 render() 方法中,删除此行 const { films } = this.props.films; 并尝试此操作: const films = this.props.films || []; 此外,请删除构造函数中的 console.log(this.props.films) 。 使用以下代码更新 FilmAction:

export function getAllFilms() {
    return dispatch => {
        return FilmServices.getAllFilms()
            .then((response) => {
                if (response) {
                    dispatch(GET_FILMS(response));
                }
            })
    }
}
Muhammad Awais
2019-11-09