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

226
Visualizações
React: Component separation with shared state

I am currently working on a React component (being quite new to React) that consists of two Fluent UI elements: A CommandBar (which holds actions such as “Add new folder”, “Delete folder”, “Upload file”…) and a DetailsList (which shows the directories and allows you to navigate by clicking on items)

The idea is to build a File Explorer that allows you to check directory tree, create new folders, upload files.

Firstly, I put the two into a single component, but as there were lots of actions, the state, and the file itself grew quite significantly. What I could move to another file I did, however there was still lots of code left (almost 700 lines) and I was soon getting lost in my own code.

So the state would look something as follows:

interface State {
 directory: string,
 columns: IColumn[]
 shouldAllowToUpload: false,
 shouldAllowToDelete: true
 ...
}

There were over 15 properties that were hard to track. So I decided to “lift the state up”, and split the components into two separate components that update state at the parent. This allowed to significantly improve the readability, but I stumbled upon a conceptual issue, which more senior developers would probably be able to solve.

The new state and props would look as follows:

interface DetailsListComponentState {
  columns: IColumn[],
  currentDirectory: ""
}

// CommandBarComponent state
interface CommandBarComponentState {
  shouldAllowToUpload: false,
  shouldAllowToDelete: true
}

// CommandBarComponent props
interface CommandBarComponentProps {
  onNewFileCreation(name: string): void,
  onFileUpload(): void
  ...
}

There are some dependencies, however, between the bar and my DetailsList as when I upload I would like to upload the file to the directory that I currently am viewing.

This would mean that I would need to lift the "directory" property to the Parent component, and would need to provide two functionalities:

  1. Provide the onDirectoryChange prop to ensure that upon navigation within the DetailsListComponent, I have the information about the directory.
  2. Keep the “directory” in Parent component, and force re-render when state "directory" property changes, which is also passed as props to the DetailsListCompoent.

Ideally, I would love to keep the directory knowledge only within the DetailsListComponent, and allow implementation of onFileUpload within Parent component to be able to access the directory information from the child.

To me, this looks not convenient as the two components are not "tightly" coupled. How would something like this be usually solved: Shared Store (Redux) or is there a way of making it work without the need of Redux.

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