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

204
Visualizações
Having an item appear conditionally in another component

I have a Nav component:

class SomeClass extends React.Component{
    
    showItem = () =>{
        console.log("itemShown");
        return(
            <Item/>
        );
    }
    
    render(){
    
        return(
            <div onClick={() => this.showItem()} >
                ABC
            </div>
        );
}

export default Nav;

I am calling it in App.js:

function App() {
  return (
      <div className="app">
          <SomeClass />
      </div>
    );
    
}

export default App;

How to have the Item component in the showItem method in SomeClass, to be shown in the App functional component.

In App.js I tried:

<div className="app">
    <SomeClass showItem={showItem} />
</div>

However, it did not work.

What is the best approach to this ?

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

0

You can manage a state inside your SomeClass component as follows. Then you can toggle it whenever you need to show or un-show the Item component.

class SomeClass extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isShowItem: false,
    };
    this.showItem = this.showItem.bind(this);
  }

  showItem = () => {
    this.setState(!this.state.isShowItem);
  };

  render() {
    return (
      <>
        <div onClick={this.showItem}>ABC</div>
        {this.state.isShowItem && <Item />}
      </>
    );
  }
}
export default SomeClass;
about 4 years ago · Juan Pablo Isaza Relatório

0

These days people don't tend to use react classes, they use functional components. Here's how you do it with functional components.

State Hook:

toggleShowItem swaps the showItem state between true and false when ever the SomeClass (which isn't a class anymore but that's what you have named yours) component's onClick is fired.

Conditional Rendering:

The ShowItem is rendered whenever the state hook's value is true.

export default function App() {
    const [showItem, setShowItem] = useState(false);

    const toggleShowItem = () => {
        setShowItem(!showItem);
    }

    return (
        <div className="App">
            <SomeClass toggleShowItem={toggleShowItem}>
                {showItem && <ShowItem />}
            </SomeClass>
        </div>
    );
}

export default function SomeClass({toggleShowItem}) {
    return <div onClick={toggleShowItem}>ABC</div>;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The better way to show the Item component is to use React State and React Props. Please read this docs since it will help you greatly on react development.

Option 1:

there will be a state in the App component for showItem. Then you pass the showItem as props in the SomeClass component.

Option 2:

the state for showItem is in the SomeClass Component, then upon clicking the toggle, the state will be updated.

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