Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
Getting problem while rendering function inside the render in React

I tried rendering gridWithNode function inside render which is not working and I am getting this error: Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it. at div at Pathfind (http://localhost:3000/static/js/bundle.js:185:74) at div at App

my code looks like this in Pathfind.js

import React, { useState, useEffect } from "react";
import Node from './Node';
import './Pathfind.css';

const rows = 5;
const cols = 5;

const Pathfind = () => {
const [Grid, setGrid] = useState([]);

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

// CREATES THE GRID
const initializeGrid = () => {
    const grid = new Array(cols);

    for (let i = 0; i < cols; i++) {
        grid[i] = new Array(rows);
    }
    createSpot(grid);

    setGrid(grid);
}

// CREATES THE SPOT 
const createSpot = (grid) => {
    for (let i = 0; i < cols; i++) {
        for (let j = 0; j < rows; j++) {
            grid[i][j] = new Spot(i, j);
        }
    }
};

// SPOT CONSTRUCTOR
function Spot(i, j) {
    this.x = i;
    this.y = j;
    this.f = 0;
    this.g = 0;
    this.h = 0;
}

// GRID WITH NODE
const gridWithNode = () => {
    <div>
        {Grid.map((row, rowIndex) => {
            return (
                <div key={rowIndex} className='rowWrapper'>
                    {row.map((col, colIndex) => {
                        return (
                            <Node key={colIndex} />
                        )
                    })}
                </div>
            )
        })}
    </div>
}
console.log(Grid);
return (
    <div className="Wrapper">
        <h1>Pathfind Component</h1>
        {gridWithNode}
    </div>
  )
}

export default Pathfind;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your function doesn't really return anything, it should return a valid jsx, so add a parenthesis around your function

// GRID WITH NODE
const gridWithNode = () => ( 
    <div>
        {Grid.map((row, rowIndex) => {
            return (
                <div key={rowIndex} className='rowWrapper'>
                    {row.map((col, colIndex) => {
                        return (
                            <Node key={colIndex} />
                        )
                    })}
                </div>
            )
        })}
    </div>
)

then invoke it inside your Wrapper div

{gridWithNode()}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!