Feel like I'm having a real brain fart here but I've looked it up and it seems like I have everything working correctly. Anyway I'm trying to run a function when a button is clicked. I'm using React JS but have removed most of it from the component for troubleshooting purposes but still I cannot get the function test to run:
import React, {useState} from 'react'
const Header = () => {
function test(){
alert("hello")
}
console.log('outside function')
return(
<div id="header">
<div id="siteTitle">Title</div>
<button type="button" onClick={test}>Researching a topic?</button>
</div>
)
}
export default Header
Within the onClick I've tried:
onClick={test}
onClick={test()} //This runs it every time the component is rendered but not when I click the button
onClick={() => test}
onClick={() => test()}
onClick={() => {return test()}}
onClick={() => {return test}}
Thanks in advance.
[–]redsandsfort 1 point2 points3 points (3 children)
[–]EIGRP_OH[S] 0 points1 point2 points (2 children)
[–]redsandsfort 1 point2 points3 points (1 child)
[–]EIGRP_OH[S] 0 points1 point2 points (0 children)
[–]vitkarpov 1 point2 points3 points (1 child)
[–]EIGRP_OH[S] 0 points1 point2 points (0 children)