Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
¿Cómo actualizaría el estado local de un componente completo en Gatsby?

Me gustaría crear una notificación simple con un botón para descartar en un sitio React estático. Digamos que tengo mi componente configurado así:

 // layout.js import * as React from 'react' const Layout = ({showNotification = true}) => <div> <header>HEADER</header> {showNotification ? <div style={{display: showNotification ? "flex" : "none"}}> NOTIFICATION <span onClick={() => {showNotification = false}}> DISMISS </span> </div> : null} <main>MAIN CONTENT</main> <footer>FOOTER</footer> </div> export default Layout

Espero que, dado que la propiedad de display se establece en función del valor de showNotification y dado que onClick actualiza el valor de showNotification, hacer clic en el intervalo "Descartar" ocultaría toda la notificación.

Sin embargo, por lo que puedo decir, se llama al controlador onClick y actualiza el valor de showNotifications, pero eso no se refleja en el renderizado.

¿Cómo puedo configurar esto para que pueda hacer que onClick actualice el estado del componente de una manera que se refleje en el resto del componente?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

showNotification es un accesorio. No se puede cambiar directamente. Además de showNotification, debe pasar una función que cambia el estado en el componente donde se establece el estado. Aquí hay un ejemplo:

 import React, {useState} from 'react' const App = () => { const [showNotification, setShowNotification] = useState(true) const toggleShow = () => setShowNotification(!showNotification) return ( <div> Some content <Layout showNotification={showNotification} toggleShow={toggleShow} /> </div> ) } export const Layout = ({showNotification, toggleShow}) => <div> <header>HEADER</header> {showNotification ? <div style={{display: showNotification ? "flex" : "none"}}> NOTIFICATION <span onClick={toggleShow}> DISMISS </span> </div> : null} <main>MAIN CONTENT</main> <footer>FOOTER</footer> </div> export default App
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!