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

204
Views
¿Cómo puedo representar condicionalmente este componente en función de si hay o no datos en el almacenamiento local?

Quiero que MyList represente lo que sea que esté en localStorage (funciona muy bien), sin embargo, cuando no hay nada en el almacenamiento local y represento el componente, aparece vacío. Me gustaría tener un marcador de posición allí para cuando eso ocurra. ¿Cómo puedo hacer eso mejor?

 const MyList = ({setDrinks}) => { const setMyList = () => { let parsedData = JSON.parse(localStorage.getItem('favoriteDrinks')) setDrinks(parsedData) } return ( <div> <button className="my-list" onClick={setMyList}>My List</button> </div> ) } export default MyList
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede asignar un marcador de posición aleatorio si su localStorage no tiene datos. Aquí hay una solución mínima:

 const MyList = ({setDrinks}) => { const setMyList = () => { const myDrinks = localStorage.getItem('favoriteDrinks'); if (!myDrinks) { setDrinks({ message: "No drinks found!" }); return; } let parsedData = JSON.parse(myDrinks); setDrinks(parsedData); } return ( <div> <button className="my-list" onClick={setMyList}>My List</button> </div> ) } export default MyList
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!