[英]JSX not being rendered
So basically, I have a component that takes data from the backend and creates nested components in a K-nary Tree format.所以基本上,我有一个组件从后端获取数据并以 K-nary Tree 格式创建嵌套组件。
Tree.js树.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import TreeNode from './TreeNode';
class Tree extends Component {
getRootNodes = () => {
return this.props.comments.filter(node => !node.cid_reference);
}
getChildNodes = (node) => {
let comments = this.props.comments;
let children = comments.filter(comment => comment.cid_reference == node.cid);
return children;
}
render() {
let rootNodes = this.getRootNodes()
return (
<div>
{console.log(rootNodes)}
{
rootNodes.map(node => (
<TreeNode
node={node}
getChildNodes={this.getChildNodes}
/>
))
}
</div>
)
}
}
const mapStateToProps = (state) => {
return {
comments: Object.values(state.comment.dbComments)
};
}
export default connect(mapStateToProps, null)(Tree);
TreeNode.js树节点.js
import React from 'react';
import { Link } from 'react-router-dom';
// Initially root nodes gets passed in
const TreeNode = (props) => {
const { node, getChildNodes, level } = props;
console.log(node);
const renderComments = (level, node) => {
let count = level;
console.log(node)
const helper = (node) => {
if (count == 0) {
console.log(count, 'returned', node)
return node;
} else {
count = count - 1;
console.log(count);
return helper(
<div className="comment">
<div className="comments">
{node}
</div>
</div>
);
}
}
/*
- if level=0, then <div className="comment">
- if level=1, then <div className="comment"><div className="comments"..
<div className="comment"
*/
helper(node);
}
return (
<React.Fragment key={node.cid}>
{renderComments(level,
<div className="comment">
<div className="content">
<a className="author">{node.username ? node.username : "annonymous"}</a>
<div className="metadata">
<span className="date">{node.date_created}</span>
</div>
<div className="text">{node.comment}</div>
<div className="actions">
<Link to={'/streams/comments/new'} className="reply">Reply</Link>
</div>
</div>
</div>
)}
{getChildNodes(node).map(childNode => (<TreeNode
{...props}
node={childNode}
level={level+1}
/>))
}
</React.Fragment>
);
}
TreeNode.defaultProps = {
level: 0,
};
export default TreeNode;
The Tree.js
is the structure of it and each component is created by TreeNode.js
. Tree.js
是它的结构,每个组件都是由TreeNode.js
创建的。 The component gets created recursively.组件是递归创建的。 The problem lies in the
renderComments
function.问题在于
renderComments
function。 In the section, return node
, nothing gets rendered to the screen.在
return node
部分中,没有任何内容呈现到屏幕上。 When I did a console.log(node)
I see that node is a React.Element which is most likely why it's not being rendered to the screen.当我执行
console.log(node)
时,我看到该节点是一个 React.Element,这很可能是它没有被呈现到屏幕上的原因。 However, if I ignore the helper
function and just return node, the comments get rendered to the screen.但是,如果我忽略
helper
程序 function 并仅返回节点,则评论将呈现到屏幕上。
As an aside, I am using Sementic-UI for the comments.顺便说一句,我正在使用 Sementic-UI 进行评论。
<div className="comment>...
gives me the regular comment while <div className="comment>...<div className="comments"
gives me a nested comment. <div className="comment>...
给我常规评论,而<div className="comment>...<div className="comments"
给我一个嵌套评论。 I'm not too sure why this doesn't work.我不太确定为什么这不起作用。 Any help would be greatly appreciated.
任何帮助将不胜感激。
you return helper
but inside helper
so actually nothing is returned as you didn't return outer helper您返回
helper
但在helper
内部,因此实际上没有返回任何内容,因为您没有返回外部助手
change this:改变这个:
const helper = (node) => {
if (count == 0) {
console.log(count, 'returned', node)
return node;
} else {
count = count - 1;
console.log(count);
return helper(
<div className="comment">
<div className="comments">
{node}
</div>
</div>
);
}
}
to be成为
return helper = (node) => {
if (count == 0) {
console.log(count, 'returned', node)
return node;
} else {
count = count - 1;
console.log(count);
return (
<div className="comment">
<div className="comments">
{node}
</div>
</div>
);
}
}
Solved thanks to @MarcBaumbach, it was just very silly mistake on my part.感谢@MarcBaumbach 解决了,这对我来说只是一个非常愚蠢的错误。
const renderComments = (level, node) => {
let count = level;
const helper = (node) => {
if (count == 0) {
return node;
} else {
count = count - 1;
return helper(
<div className="comment">
<div className="comments">
{node}
</div>
</div>
);
}
}
return helper(node); // The line that fixed the code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.