简体   繁体   中英

Objects are not valid as a React child: Trying to show my posts

I'm having problems with my React & Python project, I'm taking posts from my API and trying to present them on screen but I get this error:

Error: Objects are not valid as a React child (found: object with keys {Post}). If you meant to render a collection of children, use an array instead.

I'm using an extra package that helps me do infinite scrolling but it's not working. I believe the issue lies in the use of the concat method.

This issue started occurring when I added a POST request to my project, but this is a GET request so..

const Crypto = (props) => {
  const [posts, setPosts] = useState([]);
  const [skip, setSkip] = useState(0);
  const [hasMore, setHasMore] = useState(true);

  useEffect(() => {
    loadFunc();
  }, []);

  const loadFunc = () => {
    axios
      .get(API_CRYPTO + skip)
      .then((res) => {
        if (res.data.messages.length === 0) {
          setHasMore(false);
          return;
        }
        setPosts(posts.concat(res.data.messages)); // here is the problem!
        setSkip(skip + 20);
      })
      .catch((err) => {
        // handle error
        console.error(err);
      });
  };

  return (
          <InfiniteScroll
            dataLength={posts.length} //This is important field to render the next data
            next={loadFunc}
            hasMore={hasMore}
            loader={
              <div className="spinner">
                <PuffLoader color={"#ffcb6b"} size={50} />
              </div>
            }
            scrollableTarget="scrollableDiv"
          >
            {posts.map((post) => (
              <Post
                channel_id={post.channel_id}
                channel_pp={post.channel_image}
                message={post.message_text}
                channelName={post.channel_nickname}
                timeStamp={post.message_ts}
                moonCounter={post.likes}
                messageImage={(post.photo_params || 0 || {}).photo_url}
                postID={post._id.$oid}
                key={post._id.$oid}
              />
            ))}
          </InfiniteScroll>
  );
};

This is what my Post component returns:

<Card id="card">
        <img src={Dots} alt="dots" className="dots"/>
        <NavLink to={"/" + props.channel_id} style={{ textDecoration: 'none'}} className="pp-link">
        <img src={props.channel_pp} alt="pp" className="pp" />
        <p className="pp-user-name" to={"/" + props.channel_id}>{props.channelName}</p>
        </NavLink>
        <p className="time-elapsed">{postedAgo}</p>
         {props.messageImage ? <img className="message-img" alt="message-img" src={props.messageImage} onClick={ModalHandler}/> : null}
        <Linkify componentDecorator={componentDecorator}>
          <p className="post-body">
            {props.message}
          </p>
        </Linkify>
        <div className="interact">
        <Comment/>
        <Moon moonCounter={props.moonCounter} postID={props.postID} />
        </div>
      </Card>

Problem has been solved!

This is a server error, my new post doesn't have the same object values that the other post have, its a backend error thank you so much for helping!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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