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

279
Views
How to make an Es6 react program to count number of letters in string in input box ? I have tried above code, but am not able to show on UI

enter image description hereIn console, i am able to see the output, but on screen, it's not coming. How to input the string from user on input box, then show the results on screen? Is something wrong with the logic? Is there any better method?

import React, { useState } from 'react'

const CharCount = () => {

    const [data, setData] = useState([])
    const [print, setPrint] = useState(false)

    const count= (value)=>{
       const result = [...value].reduce((a, e) => { a[e] = a[e] ? a[e] + 1 : 1; return a }, [])
        console.log(result, typeof(result))
        return result
}
   const getData=(e)=>{
    setData(e.target.value)
    setPrint(false)
   }

  return (
    <div>
        <input type="text" value={data} onChange={getData}/>
        <button onClick={()=>setPrint(true)}>CLICK!</button>
    {
     print ? count(data):"error"
    }
    </div>
  )
}

export default CharCount
about 4 years ago · Juan Pablo Isaza
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!