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

186
Visualizações
Accessing Child Component in GrandParent component

I am building a Search Algorithm Visualizer website.

The order of components is App->Row->Box

Each box component has its own unique id (rowNumber-colNumber) and has its classname set as "Unvisited"

I have an onclick event in App Component which runs BFS

To visualize Bfs,I have to access and change some specific (based on their id) Box Component's classname to "Visited"

How can i achieve it?

These are my components


//APP COMPONENT

import React from "react"
import Row from "./Row"
export const Start = {row:14,col: 18},End = {row:14,col: 35}
export default function App(){
          const [grid,setGrid] = React.useState([])
          React.useEffect(() => {
             for(let i = 1;i<29;i++){
                 setGrid(prevArray =>prevArray.concat(<Row key = {i} row = {i}/>))
             }
          },[])
          return (
                 <>
                    <div className = "Navbar">
                        <a  href ="#" onClick = {Bfs}>Breadth First Search</a>
                    </div>
                    {grid}
                 </>
          )
}
//ROW COMPONENT 

import React from "react"
import Box from "./Box"
import {Start,End} from "./App"
export default function Row(props){
          const row = [];
          for(let colNumber = 1;colNumber<54;colNumber++){
                    row.push(<Box  
                        key = {colNumber}
                        row = {props.row}
                        col = {colNumber}
                        isStart = {props.row == Start.row && colNumber == Start.col}
                        isEnd = {props.row == End.row && colNumber == End.col}
                    />)
          }
          return (
                    <div className ="row">
                              {row}
                    </div>
          )
}
//BOX COMPONENT
 
import React from "react";
export default function Box({ row, col, isStart, isEnd }) {
  return (
           <div className="Unvisited" id={`${row}-${col}`}>
                {isStart && <img src="./images/start.png"></img>}
                {isEnd && <img src="./images/target.png"></img>}
          </div>
  )
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In App component; const grid = []

since you are using grid as a variable, react doesn't care if the grid changes; you have to use useState hook const [grid, setGrid] = useState([])

and usign setGrid you have to update the value of grid.

React will automatically rerender the component once grid is changed using setGrid.

Whenever you need to update some data on page you need to use state in react

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