Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用减速器时无法渲染组件_Reactjs_React Redux - Fatal编程技术网

Reactjs 使用减速器时无法渲染组件

Reactjs 使用减速器时无法渲染组件,reactjs,react-redux,Reactjs,React Redux,我正在使用React Redux制作一个日历应用程序,按月存储园艺任务。当我使用reducer存储“task”对象时,尽管没有得到任何控制台错误,但我无法将它们渲染到日历中。当我使用字符串传入任务名称时,它们将正确呈现 我主要是通过控制台日志在每个点进行调试,以检查传递的数据是否未定义,是否是正确的数据类型。我的应用程序的一切都和我预期的一样,似乎没有什么问题,只是它不会呈现 此不会将任务渲染到日历,但不会产生错误: render() { return ( <ul

我正在使用React Redux制作一个日历应用程序,按月存储园艺任务。当我使用reducer存储“task”对象时,尽管没有得到任何控制台错误,但我无法将它们渲染到日历中。当我使用字符串传入任务名称时,它们将正确呈现

我主要是通过控制台日志在每个点进行调试,以检查传递的数据是否未定义,是否是正确的数据类型。我的应用程序的一切都和我预期的一样,似乎没有什么问题,只是它不会呈现

不会将任务渲染到日历,但不会产生错误:

  render() {
    return (
      <ul className="task-container">
          { this.props.taskTypes.map(task => task.name).forEach(taskName => this.renderTask(taskName)) }
      </ul>
    );
  };
这是完整的容器文件(TaskGroup.js):


forEach
循环不返回任何内容。只使用
map

render() {
  return (
    <ul className="task-container">
      {this.props.taskTypes.map(task => this.renderTask(task.name)}
    </ul>
  )
}
render(){
返回(
    {this.props.taskTypes.map(task=>this.renderTask(task.name)}
) }
@Li357谢谢你发现了这一点!这是一个问题输入错误,我现在已经更正了。原始代码有花括号。谢谢!这让我发疯了!:D
export default function() {
  return [
    { name: 'Sow Indoors' },
    { name: 'Sow Outside' },
    { name: 'Plant Outside' },
    { name: 'Harvest' },
  ];
}
class TaskGroup extends Component {
  constructor(props) {
    super(props);
    this.checkAgainstMonthAndType = this.checkAgainstMonthAndType.bind(this);
    this.taskTypeIsUsed = this.taskTypeIsUsed.bind(this);
    this.taskHasItems = this.taskHasItems.bind(this);
    this.renderTaskGroup = this.renderTaskGroup.bind(this);
    this.renderTask = this.renderTask.bind(this);
    this.renderTaskItems = this.renderTaskItems.bind(this);
  }

  checkAgainstMonthAndType(list, givenMonth, givenType) {
    return list.some(item => item.month === givenMonth && item.type === givenType);
  }

  taskTypeIsUsed(list, givenMonth, givenType) {
    return list.some(item => this.checkAgainstMonthAndType(item.tasks, givenMonth, givenType));
  };

  taskHasItems(list, givenMonth, givenType) {
    return this.checkAgainstMonthAndType(list, givenMonth, givenType);
  };

  renderTask(taskType) {
    const taskClass = taskType.toLowerCase().replace(" ", "-"); // Sow Indoors -> sow-indoors

    if( this.taskTypeIsUsed(this.props.data, this.props.thisMonth, taskType) ) {
      return (
        <li key={`key-${taskClass}`} className={`task task--${taskClass}`}>
          <h3 className={`task__title task__title--${taskClass}`}>{taskType}</h3>
          <ul className="task__item-list">
            {this.renderTaskItems(taskType)}
          </ul>
        </li>
      );
    }
  };

  renderTaskItems(taskType) {
    return this.props.data
      .filter(item => this.taskHasItems(item.tasks, this.props.thisMonth, taskType))
      .map(item =>
        <Task key={`task-${item.name}`} variety={item.variety} name={item.name} />
      );
  };

  render() {
    return (
      <ul className="task-container">
        {this.renderTask("Sow Indoors")}
      </ul>
    );
  };

}

function mapStateToProps(state) {
  return {
    taskTypes: state.taskTypes,
    data: state.data
  }
}

TaskGroup.propTypes = {
  thisMonth: PropTypes.string.isRequired,
  taskTypes: PropTypes.array.isRequired,
  data: PropTypes.array.isRequired,
};

export default connect(mapStateToProps)(TaskGroup);
{
      "id": 0,
      "name": "Peas",
      "variety": "Sugarsnap",
      "tasks": [
        {
          "type": "Sow Indoors",
          "month": "Feb"
        },
        {
          "type": "Plant Outside",
          "month": "Apr"
        },
        {
          "type": "Harvest",
          "month": "May"
        }
      ]
    }
render() {
  return (
    <ul className="task-container">
      {this.props.taskTypes.map(task => this.renderTask(task.name)}
    </ul>
  )
}