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

257
Views
¿Hay alguna modificación que pueda aplicar a este código para hacerlo más limpio?

Entonces, en mi viaje de aprendizaje, construí esta aplicación simple, muestra una cotización de una API y la muestra con cada clic de botón, tuve problemas cuando intenté mostrar la cotización y usé algo de vanilla. JavaScript. Aquí está mi código

 function App() { const [error , setError] = useState(null); const [isLoaded ,setIsLoaded] = useState(false); const [dataQuotes , setDataQuotes] = useState([]); useEffect(() => { fetch('https://type.fit/api/quotes') .then(res => res.json()) .then( (results) => { setIsLoaded(true) setDataQuotes(results) }, (error) => { setIsLoaded(true) setError(error) } ) },[]) function getQuote(){ const Quotes = dataQuotes const randomNumber = Math.floor(Math.random()*Quotes.length) const finalData = Quotes[randomNumber]?.text document.getElementById('content').innerHTML=`"${finalData}"` } if (error) { return <div>Error :{error.message}</div> } else if (!isLoaded) { return <div>Loading...</div> } else { return ( <> <div className='container'> <h3 id='content'>{}</h3> </div> <button onClick={getQuote} className='btn'>Generate Quotes</button> </> ); } }

¿Hay alguna modificación que pueda aplicar para que el código sea más limpio, porque siento que puedo hacerlo mejor y más limpio?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

La mayor parte ya se ve limpia. He señalado algunos cambios a continuación.

 function App() { const [error , setError] = useState(null); const [isLoaded ,setIsLoaded] = useState(false); const [dataQuotes , setDataQuotes] = useState([]); const ref = useRef(); useEffect(() => { fetch('https://type.fit/api/quotes') .then(res => res.json()) .then( (results) => { setIsLoaded(true) setDataQuotes(results) }, (error) => { setIsLoaded(true) setError(error) } ) },[]) function getQuote(){ const Quotes = dataQuotes const randomNumber = Math.floor(Math.random()*Quotes.length) const finalData = Quotes[randomNumber]?.text //document.getElementById('content').innerHTML=`"${finalData}"` ref.current.innerHTML = finalData } return ( <> {error ? <div>Error :{error.message}</div> : !isLoaded ? <div>Loading...</div> : <> <div className='container'> <h3 id='content' ref={ref}>{}</h3> </div> <button onClick={getQuote} className='btn'>Generate Quotes</button> </> } </> ); } }
about 4 years ago · Santiago Gelvez Report

0

Tienes dos veces setIsLoaded(true) : una en .then y otra en .error . Puede usar .finally(() => { setIsLoaded(true) } .

about 4 years ago · Santiago Gelvez 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!