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

150
Visualizações
React dynamic view change how to get it to re-render?

I am trying to change views dynamically based on user click

I understand why the component does not re-render, but I cannot see how to best make re-render without force update function.

I dont think I can useState since it will be outside of function component.

Have the following code in Structure.ts:


import React from 'react'

export class Node {

    private _nodeName : string;
    private _reactElement: JSX.Element;


    constructor(nodeName:string, reactElement:JSX.Element){
        this._nodeName = nodeName;
        this._reactElement = reactElement;
    }

    public render(){
        return this._reactElement;
    }


}


export class ViewStructure{

    private  _viewNodeList: Node[] =[];
  
    private  _currentViewNode: Node;

    public setNode(index:number){

    }

    public next(){
        debugger;
        let currentNodeIndex = this._viewNodeList.findIndex((node) => node === this._currentViewNode);

        let nextNodeIndex = currentNodeIndex +1;
        this._currentViewNode = this._viewNodeList[nextNodeIndex];
    }

    public previous(){
        console.log('cicked previous')
    }
  

    public addNode(viewNode:Node){
        if(!this._currentViewNode){
            this._currentViewNode = viewNode;
        }
        this._viewNodeList.push(viewNode);
    }

    public render(){
        return this._currentViewNode.render();
    }
  
}


and main react component App.tsx

import React, {useState, useRef, useEffect} from 'react'

import { ViewNode, ViewStructure } from "./Structure";


import SomeComponent from './SomeComponent';
import SomeComponent2 from './SomeComponent2';


export default function App() {


 var NodeViewStructure = new  ViewStructure();
 
var personalInfo = new ViewNode("Screen2", <SomeComponent2 ></SomeComponent2> );
var intro = new ViewNode('Screen 1',  <SomeComponent name="John" age={22}></SomeComponent> );
 



NodeViewStructure.addNode(intro);
NodeViewStructure.addNode(personalInfo);


useEffect(()=>{
console.log('just rendered');
debugger;
}, [])

function nextView(){
  console.log('next');
  debugger;
  NodeViewStructure.next();
}



    return (
   

      <div className="App">
       <span > {NodeViewStructure.render()} </span>
        <button onClick={() =>nextView()}>Next </button>
      </div>

 
    );

}
  







about 4 years ago · Juan Pablo Isaza
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