reactjsHow can I use TypeScript with React.js to define an onClick event?
In order to use TypeScript with React.js to define an onClick event, you need to first create a React component and then define the onClick event as a function inside the component. The function should have an event parameter of type React.MouseEvent.
import React, { MouseEvent } from 'react';
interface Props {
onClick: (e: MouseEvent) => void;
}
const MyComponent: React.FC<Props> = ({ onClick }) => {
return <button onClick={onClick}>Click me!</button>;
};
export default MyComponent;
The onClick function can then be passed to the React component as a prop.
import React from 'react';
import MyComponent from './MyComponent';
const App = () => {
const handleClick = (e: React.MouseEvent) => {
console.log('Button was clicked!');
};
return <MyComponent onClick={handleClick} />;
};
export default App;
The output of this example code will be a button that when clicked, will log "Button was clicked!" to the console.
Code explanation
-
import React, { MouseEvent } from 'react';
- This imports the React library and the MouseEvent type from the React library. -
interface Props { onClick: (e: MouseEvent) => void; }
- This defines the props for the React component, with the onClick prop being a function that takes a MouseEvent parameter and returns nothing. -
const MyComponent: React.FC<Props> = ({ onClick }) => {...}
- This is the React component that takes the onClick prop as a parameter and renders a button that calls the onClick function when clicked. -
const handleClick = (e: React.MouseEvent) => {...}
- This is the function that will be called when the button is clicked. -
return <MyComponent onClick={handleClick} />
- This passes the handleClick function as a prop to the MyComponent component.
Helpful links
More of Reactjs
- How can I use zxcvbn in a ReactJS project?
- How do I use ReactJS to create an example XLSX file?
- How can I use ReactJS to zoom in and out of elements on a page?
- How can I use ReactJS and XState together to create a state machine?
- How do I create a zip file using ReactJS?
- How can I convert an XLSX file to JSON using ReactJS?
- How do I zip multiple files using ReactJS?
- How can I use ReactJS Zustand to manage state in my application?
- How can I fix the "process is not defined" error when using ReactJS?
- How can I use a ReactJS XML editor?
See more codes...