Возможно, это очень простое исправление, но я не смог понять, почему функция карты не распознает входные данные как массив.
У меня есть модель дилеммы, которая содержит массив комментариев пользователей. Я получаю данные и сопоставляю их с состоянием моего реагирующего компонента. Затем я пытаюсь проанализировать данные как реквизиты для подкомпонента, где я пытаюсь отобразить данные.
Редуктор дилеммы
const initialState = {
loading: false,
dilemma: {}
};
export default function(state = initialState, action) {
switch (action.type) {
case GET_DILEMMA:
return {
...state,
dilemma: action.payload,
loading: false
};
case GET_DILEMMAS:
return {
...state,
dilemmas: action.payload,
loading: false
};
case CREATE_DILEMMA:
return {
...state,
dilemmas: [action.payload, ...state.dilemmas]
};
case DELETE_DILEMMA:
return {
...state,
dilemmas: state.dilemmas.filter(
dilemma => dilemma._id !== action.payload
)
};
case DILEMMAS_LOADING:
return {
...state,
loading: true
};
default:
return state;
}
}
this.props в компоненте с состоянием
{
"match": { "path": "/", "url": "/", "isExact": true, "params": {} },
"location": { "pathname": "/", "search": "", "hash": "", "key": "crpcmj" },
"history": {
"length": 50,
"action": "POP",
"location": { "pathname": "/", "search": "", "hash": "", "key": "crpcmj" }
},
"dilemmas": {
"loading": false,
"dilemma": {
"red_votes": 0,
"blue_votes": 0,
"_id": "5b855fcbdfa436e0d25765fa",
"user": "5b855f9fdfa436e0d25765f9",
"prefix": "Hvis du skulle vælge",
"title": "Svede remoulade",
"red": "Gå med rustning resten af dit liv",
"blue": "Svede remoulade resten af dit liv",
"likes": [],
"comments": [
{
"date": "2018-08-28T17:28:23.340Z",
"_id": "5b858637b6f6a6e6218eeaba",
"user": "5b855f9fdfa436e0d25765f9",
"text": "This is a test3 comment",
"author": "Albyzai"
},
{
"date": "2018-08-28T17:28:19.915Z",
"_id": "5b858633b6f6a6e6218eeab9",
"user": "5b855f9fdfa436e0d25765f9",
"text": "This is a test2 comment",
"author": "Albyzai"
},
{
"date": "2018-08-28T15:50:18.792Z",
"_id": "5b856f3aed156de48a270766",
"user": "5b855f9fdfa436e0d25765f9",
"text": "This is a test comment",
"author": "Albyzai"
}
],
"date": "2018-08-28T14:44:27.413Z",
"slug": "svede-remoulade",
"__v": 3
}
},
"errors": {}
}
Компонент с состоянием
import React, { Component } from "react";
import propTypes from "prop-types";
import { connect } from "react-redux";
import { getDilemma, addLike, removeLike } from "../../actions/dilemmaActions";
import Dilemma from "./../dilemma/Dilemma";
import Divider from "./../dilemma/Divider";
import CommentFeed from "../dilemma/CommentFeed";
class DilemmaLayout extends Component {
constructor() {
super();
this.state = {
title: "",
prefix: "",
red: "",
blue: "",
red_votes: 0,
blue_votes: 0,
likes: [],
comments: [],
errors: {}
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.errors) {
this.setState({ errors: nextProps.errors });
}
if (nextProps.dilemmas.dilemma) {
const dilemma = nextProps.dilemmas.dilemma;
this.setState({
id: dilemma._id,
user: dilemma.user,
title: dilemma.title,
prefix: dilemma.prefix,
red: dilemma.red,
blue: dilemma.blue,
red_votes: dilemma.red_votes,
blue_votes: dilemma.blue_votes,
likes: [dilemma.likes],
comments: [dilemma.comments]
});
}
}
render() {
return (
<div>
<CommentFeed comments={this.state.comments} />
</div>
);
}
}
DilemmaLayout.propTypes = {
getDilemma: propTypes.func.isRequired,
addLike: propTypes.func.isRequired,
removeLike: propTypes.func.isRequired,
dilemmas: propTypes.object.isRequired
};
const mapStateToProps = state => ({
dilemmas: state.dilemmas,
errors: state.errors
});
export default connect(
mapStateToProps,
{ getDilemma, addLike, removeLike }
)(DilemmaLayout);
Функциональный компонент для приема реквизита
import React from "react";
import Comment from "./Comment";
import { Container } from "reactstrap";
const CommentFeed = comments => {
const commentArray = comments.map(comment => {
<Comment author={comment.author} text={comment.text} />;
});
return <Container>{commentArray}</Container>;
};
export default CommentFeed;