Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda