简体   繁体   English

嵌入功能组件并从那里返回列表时,React应用程序中的渲染错误未返回任何内容

[英]Nothing was returned from render error in React application when embedding a functional component and returning list from there

I have the following component structure. 我具有以下组件结构。 But when I run the site I get "Nothing was returned from render." 但是,当我运行该网站时,我得到“渲染未返回任何内容”。 error. 错误。

Files: 档案:

Layout.jsx Layout.jsx

import React, { Component } from "react";
import "./Layout.css";
import axios from "axios";
import Posts from "../../components/Posts/Posts";

class Layout extends Component {
  /**
   * Set a state
   */
  state = {
    posts: []
  };

  /**
   * Import data
   */
  componentDidMount() {
    axios.get("http://jsonplaceholder.typicode.com/posts").then(response => {
      this.setState({ posts: response.data });
    });
  }

  /**
   * Render
   */
  render() {
    return (
      <div className="BlogContainer">
        <Posts postList={this.state.posts} />
        <div className="fullpost">
          <h1>Full Post (Name)</h1>
        </div>
      </div>
    );
  }
}

export default Layout;

Posts.jsx Posts.jsx

import React from "react";
import Post from "./Post/Post";

const posts = props => {
  props.postList.map(post => {
    return (
      <div className="teasers">
        <h1>Recent Posts</h1>
        <Post title={post.title} content={post.body} key={post.id} />
      </div>
    );
  });
};

export default posts;

Post.jsx Post.jsx

import React from "react";

const post = props => {
  return (
    <div className="teaser" key={props.key}>
      <h3>{props.title}</h3>
      <p>{props.content}</p>
    </div>
  );
};

export default post;

Error message: 错误信息:

posts(...): Nothing was returned from render. posts(...):渲染未返回任何内容。 This usually means a return statement is missing. 这通常意味着缺少return语句。 Or, to render nothing, return null. 或者,不渲染任何内容,则返回null。

From Layout.jsx . Layout.jsx

However, when I remove <Posts ... /> the error goes away. 但是,当我删除<Posts ... /> ,错误消失了。

If I run the map loop directly in Layout.jsx instead of embedding <Posts /> functional component, everything works well - like the following: 如果我直接在Layout.jsx运行map循环,而不是嵌入<Posts />功能组件,那么一切都会很好-如下所示:

render() {
    return (
      <div className="BlogContainer">
        <div className="teasers">
          {this.state.posts.map(post => {
            return (
              <div className="teaser" key={post.id}>
                <h3>{post.title}</h3>
                <p>{post.body}</p>
              </div>
            );
          })}
          ;
        </div>
        <div className="fullpost">
          <h1>Full Post (Name)</h1>
        </div>
      </div>
    );
  }

What went wrong in my approach? 我的方法出了什么问题?

You are missing the return in your posts component 您缺少帖子组件中的return

Posts.jsx Posts.jsx

import React from "react";
import Post from "./Post/Post";

const posts = props => {
  return props.postList.map(post => {
    return (
      <div className="teasers">
        <h1>Recent Posts</h1>
        <Post title={post.title} content={post.body} key={post.id} />
      </div>
    );
  });
};

export default posts;

or you could skip the return but also remove the wrapping {} 或者,您可以跳过return但也可以删除包装的{}

import React from "react";
import Post from "./Post/Post";

const posts = props => props.postList.map(post => (
      <div className="teasers">
        <h1>Recent Posts</h1>
        <Post title={post.title} content={post.body} key={post.id} />
      </div>
    ));

export default posts;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM