enter image description here Its a program , which counts no. of characters on input string. everything is coming correct on console, but i am not able to get it on UI. I have tried getting count.valueOf() , but it doesnot seems right.
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 }, [])
setPrint(false)
console.log(result)
return result
}
const clickHandler=()=>{
setPrint(true)
}
return (
<div>
<input type="text" value={data} onChange={ e=> setData(e.target.value)}/>
<div><button onClick={clickHandler}>CLICK!</button></div>
{
print?
<h1>
{count(data)}
</h1> : ""
}
</div>
)
}
export default CharCount
count(data) return an array , try this
print && (
<>
count(data).map(val => (<h1>{val}</h1> ))
</>
)