I am building a React.JS website and I am not entirely sure how to implement a section of JavaScript inside of my html. In normal HTML you would put the script inside of these tags:
<Script></Script>
But with react.js
function App() {
return (
<div>
<button onlick='log()'>Log</button>
<script>
Log() = Console.log('Hello')
</Script>
</div>
);
}
export default App;
Something like this will not work. How am i able to run a script this way?
Please learn how React works and how events work specifically
function App() {
const log = () => console.log('Hello')
return <button onClick={log}>Log</button>
}
export default App;
try this
function App() {
const log =()=> {console.log('Hello')}
return (
<div>
<button onClick={()=>log()}>Log</button>
</div>
);
}
export default App;
more information: https://reactjs.org/docs/handling-events.html
ReactJs Docs will be your friend here but basically you can run your javascript code inside your function component before your return statement
Here is a codesandbox link example