【发布时间】:2018-08-28 18:36:17
【问题描述】:
这可能是一个非常简单的修复,但我无法弄清楚为什么 map 函数无法将输入识别为数组。
我有一个 Dilemma 模型,其中包含一组用户 cmets。我获取数据并将其映射到我的反应组件的状态。然后我尝试将数据作为道具解析到我尝试映射数据的子组件。
困境减少器
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;
【问题讨论】:
-
在您的 CommentFeed 函数中,console.log 记录 cmets 以查看它是什么...
-
它返回
{"comments":[]},所以它必须是我将它解析为导致问题的子组件的方式。 -
是的(来自下面的回答 cmets)请发布您的减速器等 - cmets/困境有问题...
-
我已经使用有状态组件中 this.props 中的 JSON 数据以及 reducer 更新了帖子。
-
StackBlitz 示例有帮助吗?我认为这真的归结为一些基本的条件渲染。
标签: arrays json reactjs react-redux