score:0
For functions like onClick, you have to describe input arugments and output or just write Function (although for better type checking you shouldn't just write Function).
So something like this:
onClick: () => void
If for example your function gets a number and returns a string, you should write it like this:
onClick: (n: number) => string
score:0
Parent Component:
interface OnClickIdInterface {
(): void,
}
export interface parentPropsInterface {
onClick?: OnGetIdInterface;
}
render() {
return <ChildComponent onClick={(id) => this.onClick(id)}/>
}
Child Component:
const HomePresentation: React.FunctionComponent<parentPropsInterface> = ({onClick}) => {
return (
<div onClick={()=>onClick()}>
some content
</div>
)
}
score:0
Following code should work. But it depends on ButtonComponent
. Used React.MouseEventHandler<T = Element>
as type.
export interface ChildComponentProps {
onClick: React.MouseEventHandler<ButtonComponent>
}
export function ChildComponent({onClick}: ChildComponentProps) {
...
return (
<div>
...
<ButtonComponent
onClick={() => onClick()}
...
/>
...
);
}
Please notify me if this is incorrect.
Source: stackoverflow.com
Related Query
- Send onClick function as props to a component in React with Typescript
- Typescript isn't complaining when a React Functional Component (React.FC<Props>) with wrong props is send as a prop?
- Render React component with onClick and execute props function
- How to use jest.spyOn with React function component using Typescript
- React Typescript how send props and use it in child component
- Use TypeScript Enum with PropTypes to define React Component props
- Is it possible to type a React function component with TypeScript without using arrow functions?
- How to test with jest and typescript with types a basic react function component
- Pass component props in Private Route with Typescript and React
- How to handle typescript function overloading with React Props
- How to send props to a component in react with conditional?
- TypeScript compiler error with React Stateless Function component
- Why does destructuring props in react with Typescript function require passing an object?
- React TypeScript declare interface with props of component
- React typescript with react-redux component requires all passed props
- Typescript React component with function overload and generic params
- React function component prop default with TypeScript
- Correct typings for React Typescript Button props with named function
- React with typescript passing function as props using hooks
- React function component not updating with new props
- Issues with props in a function component with TypeScript
- Default property value in React component with extended props using TypeScript 3
- How to call a function with arguments onclick of a button in a react component
- How to test a React Component with Props in TypeScript using Jest?
- React functional component with mapped Child functional component with onClick function
- Cannot assign a function on onClick property of React with Typescript
- React functional <a> component with onClick and TypeScript
- How to test react component with onClick function Jest/React testing library
- Pass down component with props as prop in React Typescript
- How do I send a function through props in react typescript
More Query from same tag
- Firefox does not animate when <g> component attributes change
- Why is the product search filter not working in the Thinking in React tutorial
- React useEffect with useState and setInterval
- React - Json Schema Form dropdowns won't load initally unless I use SetTimeout function
- Open one Submenu at a time React js
- How to transport a function and an object to a functional component in react
- Why is React Js called as Single Page Application
- Javascript - object keys / values
- Initial default state is not showing, displaying empty
- React undefined map output
- ReactJS: How can I take a JSON entry from user and post into API?
- npx create-react-app client gives this error "Cannot read properties of undefined (reading 'isServer')"
- Join group on the js - SignalR 1.1.4
- Base type on object
- React/WordPress PWA does not respond with a 200 when offline
- How do I refresh my component when clicked on tab on bottomTabNavigator in React Native, in React Navigation v3?
- React-Bootstrap Collapse not working with custom components
- Docusaurus V2 - Activating Google Analytics hides navbar and footer
- Add transition to changin text in javascript
- Unshuffle the questions on javascript
- Next.js: How to use source-map-explorer with Next.js
- Debugging styled-components in typescript
- Reactjs how to change the state of a component from a different component
- Redux: local state id's and/or api uuid's
- So how can I sum all the values in all objects in an array which is coming from react redux?
- Buttons toggle in React
- Unhandled Rejection (TypeError): Cannot read property '0' of undefined
- Render an Element with React
- What is the right way to manage synthetic events using hooks and update the state from event handlers?
- Replicating color box in VScode when writing CSS