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

102
Visualizações
Using state in other component

Hi I am learning and new to react and I want to know how to pass state from one component to other,

I have one component as

const [paneCount, setPaneCount]= useState(1);

const openPane = (paneKey) => {
  setOpeningPaneKeys(oldState => {
    if (!oldState.includes(paneKey)) {
      return [...oldState, paneKey]
    }
    return oldState
  })
  setPaneCount(paneCount+1);
  console.log(paneCount);

  setFocusingPaneKey(paneKey)
}

where I want to use paneCount in App.js file

function App(props) {

const [inactive, setInactive] = useState(false);

return (
<div className="App">
  
  <Header />
  <Navbar 
      onCollapse={(inactive) => {
        setInactive(!inactive);
      }}
    />

  <div class="landing-card">
    <div>
      
      <h4 class="headingStyle">Recorder Box</h4>
      <h4>Count:{props.paneCount}</h4>
      <img src="landing.jpg" alt="Forest" width="775" height="500"></img>
    </div>
  </div>

How to pass paneCount to App.js

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can't pass data upwards, only downwards. The reason for that is how the application and data flow is built. Luckily there are 3 ways to get it done.

1) Initialize in App.js

If you want to use state value in App.js but want to work with it somewhere else:

export default function App() {
  const [myState, setMyState] = useState();

  return <Component state={ myState } setState={ setMyState } />
}

This way you can keep all your state in 1 place and use it everywhere, but it also means that you have to pass component by component to do so.

2) Context

This improves the previous option, because you no longer need to pass data around. Instead you can keep it in a provider and use it throughout your application.

There are multiple ways to define a provider, so I will just link you the docs for that one.

Note: Provider definitions does not differ in any way. They are not practical or impractical, simply one's preference over another!

3) Redux

This is arguably the BEST option for state management (eventhough I don't like it..). I haven't used it yet and don't want to either, because the previous option does the same with less effort IMO.

Here's the docs for that one.

about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of passing paneCount to App.js which I suppose is the parent component. You can create paneCount in App.js and then pass setPaneCount to the openPane component like this.

<openPane setPaneCount = {setPanecount} paneCount = {setPaneCount}/>

If you are not calling openPane in the App.js for some reason then go to the parent component that is calling both App and openPane and create and pass the setPaneCount and paneCount from there. If you making something complex instead of drilling the value down like this you might want to look at some stateManagement tools like Redux or Context API.

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