The issue isn't when passing the function, it's when destructuring the props and providing a default function:
{ createCard = f => f }: dataFormProps
This code indicates that createCard should be a function which accepts a parameter and returns a value. Which it doesn't:
createCard: ()=>void
Make the default parameter match the signature:
{ createCard = () => {} }: dataFormProps
Answer from David on Stack OverflowThe issue isn't when passing the function, it's when destructuring the props and providing a default function:
{ createCard = f => f }: dataFormProps
This code indicates that createCard should be a function which accepts a parameter and returns a value. Which it doesn't:
createCard: ()=>void
Make the default parameter match the signature:
{ createCard = () => {} }: dataFormProps
default function createCard should has type () => void. So just update like this:
DataForm({ createCard = () => {} }: dataFormProps)
Pass function with parameters as prop
How to pass a function as a prop in React+Typescript?
Passing a function as a prop to a Typescript React Functional Component
Function component props with typescript
Videos
export default function Child(props) {
const handleClick = () => {
//do something
const parentFunctionParam = props.parentFunctionParameter;
props.callParentFunction(parentFunctionParam);
}
return( <Button onClick={handleClick} />);
}
export default function Parent() {
const someFunction(someParam) => {
// do something
}
return (<Child callParentFunction= {()=> someFunction(someParam)} />);
}
So, as stated above I have one Parent component and a Child component. I need to pass a function as prop from Parent to Child. The function has some parameters, do I also need to pass the parameters explicitly as prop from Parent to Child?
You need to declare the prop type in Search Component and declare type to parameter too:
//use this type to both components (children and parent)
interface FuncProps {
//here you can declare the return type (here is void)
handleLocationChange: (values: any) => void;
}
//children start
// here are the tip, define the type in the React.FC and in the FC's parameters itself
const Search: React.FC<FuncProps> = (props: FuncProps) => {
... your component behavior and view ...
return (
{/*↓↓↓↓ use the prop like this ↓↓↓↓*/}
<input onClick={props.handleLocationChange('something if you need')}/>
)
};
//children end
// parent start
const Sidebar: React.FC<Props> = (props: FuncProps) => {
return (
<>
<Search handleLocationChange={props.handleLocationChange} />
</>
)
}
//parent end
I hope this answer can help who wants to use typescript and want to easy your own life passing functions through components (I don't recomend pass functions through many levels).
You need to declare handleLocationChange as a prop on the Search component
Hey guys, do you see any disadvantages about writing:
const Component = ({
text
}:{
text: string
}) => {
return (
<div>{text}</div>
)
}instead of:
interface ComponentProps {
text: string
}
const Component = ({text}: ComponentProps) => {
return (
<div>{text}</div>
)
}<MyButton name="My Button" clickFunction={MyPublicFunction(1)} >
The expression MyPublicFunction(1) is immediately invoked during evaluating of the containing expression. What you want is to provide a function to clickFunction:
<MyButton name="My Button" clickFunction={() => MyPublicFunction(1)} >
Note that you would get a type error if you had written something like this:
interface myProps {
name: string;
clickFunction: () => void;
}
this method worked for me:
The parent:
class App extends React.Component<Props, State> {
greet() {
alert('Hello!')
}
render() {
return (
<div className="col-xs-10 col-xs-offset-1">
<Home greet={this.greet}/>
</div>
)
}
}
The child:
interface Props {
greet: () => void
}
export class Home extends React.Component<Props, State> {
constructor(props: any) {
super(props)
}
render() {
return (
<button className="btn btn-warn" onClick={this.props.greet}>Greet</button>
)
}
}