I am new to REACT and trying to use array.find function. Getting error Cannot read property first_name of undefined.
When I declare JSON array inside RenderIt function. Not sure, why REACT cannot see results array.
export default function App() {
const [results, SetResults] = useState([])
useEffect(() => {
const JSON = [{
EMPLID: '345386',
first_name: 'David',
},
{
EMPLID: '345397',
first_name: 'Luca',
},
]
SetfName(JSON[0].first_name)
SetResults(JSON)
}, [])
const RenderIt = () => {
let found = results.find(element => element.EMPLID === '345397')
return ( < div > {
found.first_name
} < /div>)
}
return ( <
div className = "App" >
<
h1 > First Name < /h1> <
h2 > {
(RenderIt())
} < /h2> <
/div>
);
}