背景颜色未呈现给创建的节点。

huangapple go评论62阅读模式
英文:

Background color is not rendering for the created nodes

问题

I am trying to implement Path Finding Visualizer tutorial by Clement. I am new with the react. The color for the start and the end node is not getting rendered.

PathVisualizer.css :

.grid{
    margin: 500px 500 500;
    margin-top: 100px;
}

PathVisualizer.jsx :

import React, {Component} from "react";
import Node from './Node/Node';
import './Node/Node.css';

import './PathfindingVisualizer.css';


export default class PathfindingVisualizer extends Component{
    constructor(props){
        super(props);
        this.state={
        nodes: [],
        };
    }
    componentDidMount() {
        const nodes=[];
       
        for(let row=0; row<20; row++){
            const currentRow=[];
            for(let col=0; col<50; col++){
                const currentNode={
                    col,
                    row,
                    isStart : row === 10 && col === 5,
                    isFinish : row === 10 && col === 45 ,
                };
                
                currentRow.push(currentNode);
            }
            nodes.push(currentRow);
        }
        this.setState({nodes});
    }
    render(){
        const {nodes}=this.state;
        console.log(nodes);

        return(
            <div className="grid">
                {nodes.map((row,rowIdx)=>{
                    return(
                        <div key={rowIdx}>
                            {row.map((node,nodeIdx) => {
                            const {isStart, isFinish} = node;
                            return(
                                
                                <Node>
                                    key={nodeIdx}
                                    isStart={isStart}
                                    isFinish={isFinish}
                                    test={'foo'}
                                    test={'kappa'}
                                </Node>
                            );
                        })}
                        </div>
                    );
                    })}
                
            </div>
        );
    }
}

Node.css :

.node {
    width: 25px;
    height: 25px;
    grid-gap: 20px;
    outline: 1px solid rgb(94, 93, 93);
    display: inline-block;
  }

.node-finish {

  background-color: rgba(181, 6, 6, 0.751) !important;
}

.node-start {
  background-color: rgb(4, 178, 4)!important;
}

Node.jsx :

import React, {Component} from "react";

import './Node.css';

export default class Node extends Component{
    constructor(props){
        super(props);
        this.state={}
    }

    render(){
        const {isFinish, isStart} = this.props
        const extraClassName = isFinish 
        ? 'node-finish'
        : isStart ? 'node-start'
        : '';
        return <div className={`node ${extraClassName}`}></div>
        
    }

}

export const DEFAULT_NODE={
    row:0,
    col:0,
}; 

These are my files. I am getting the output of the grid rendered correctly. But the node color for that specific mentioned nodes are not changing. Please help me out in the same.

Thanks.

英文:

I am trying to implement Path Finding Visualizer tutorial by Clement . I am new with the react. The color for the start and the end node is not getting rendered.

Please take a look at my files:

PathVisualizer.css :

.grid{
margin: 500px 500 500;
margin-top: 100px;
}

PathVisualizer.jsx :

import React, {Component} from &quot;react&quot;;
import Node from &#39;./Node/Node&#39;;
import &#39;./Node/Node.css&#39;
import &#39;./PathfindingVisualizer.css&#39;;
export default class PathfindingVisualizer extends Component{
constructor(props){
super(props);
this.state={
nodes: [],
};
}
componentDidMount() {
const nodes=[];
for(let row=0; row&lt;20; row++){
const currentRow=[];
for(let col=0; col&lt;50; col++){
const currentNode={
col,
row,
isStart : row === 10 &amp;&amp; col === 5,
isFinish : row === 10 &amp;&amp; col === 45 ,
};
currentRow.push(currentNode);
}
nodes.push(currentRow);
}
this.setState({nodes});
}
render(){
const {nodes}=this.state;
console.log(nodes);
return(
&lt;div className=&quot;grid&quot;&gt;
{nodes.map((row,rowIdx)=&gt;{
return(
&lt;div key={rowIdx}&gt;
{row.map((node,nodeIdx) =&gt; {
const {isStart, isFinish} = node;
return(
&lt;Node&gt;
key={nodeIdx}
isStart={isStart}
isFinish={isFinish}
test={&#39;foo&#39;}
test={&#39;kappa&#39;}
&lt;/Node&gt;
);
})}
&lt;/div&gt;
);
})}
&lt;/div&gt;
);
}
}

Node.css :

.node {
width: 25px;
height: 25px;
grid-gap: 20px;
outline: 1px solid rgb(94, 93, 93);
display: inline-block;
}
.node-finish {
background-color: rgba(181, 6, 6, 0.751) !important;
}
.node-start {
background-color: rgb(4, 178, 4)!important;
}

Node.jsx :

import React, {Component} from &quot;react&quot;;
import &#39;./Node.css&#39;;
export default class Node extends Component{
constructor(props){
super(props);
this.state={}
}
render(){
const {isFinish, isStart} = this.props
const extraClassName = isFinish 
? &#39;node-finish&#39;
: isStart ? &#39;node-start&#39;
: &#39;&#39;;
return &lt;div className ={`node ${extraClassName}`}&gt;&lt;/div&gt;
}
}
export const DEFAULT_NODE={
row:0,
col:0,
}; 

These are my files. I am getting the output of the grid rendered correctly. But the node color for that specific mentioned nodes are not changing. Please help me out in the same.

Thanks.

答案1

得分: 1

我对此很好奇。我在我的一侧尝试了颜色渲染,它运行良好。
您应该检查节点,isStart 和 isFinish 属性。

哦,这里的代码有误。

<Node
   key={nodeIdx}
   isStart={isStart}
   isFinish={isFinish}
   test={'foo'}
   test={'kappa'}>
</Node>
英文:

I am very curious about this. I tried color rendering on my side but it works well.
You should check the nodes, isStart and isFinish props.

Oh you have wrong code in here.

&lt;Node&gt;
key={nodeIdx}
isStart={isStart}
isFinish={isFinish}
test={&#39;foo&#39;}
test={&#39;kappa&#39;}
&lt;/Node&gt;

This should be like below.

&lt;Node
key={nodeIdx}
isStart={isStart}
isFinish={isFinish}
test={&#39;foo&#39;}
test={&#39;kappa&#39;}&gt;
&lt;/Node&gt;

huangapple
  • 本文由 发表于 2023年5月17日 06:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定