Proper way of passing function from one component to other on React typescript

Sanket Patil

I am new to typescript, I was facing problems while passing the function of the parent component as a prop to another component, I searched online but nothing was useful

For reference, I am attaching JSX Code here, [TSX is what I wish]

This is parent component

export default function Notification() {
    const [notification, setnotification] = useState(['alert', 'booking'])

    return (
        <div>
            <ShowNotification setnotification={setnotification} notification={notification} />
        </div>
    )
}

This is child component

const ShowNotification = ({notification,setnotification}) => {
    notification.map((item, index) => {
        <li>item</li>
    })
}

The main issue for me is specifying prop types in typescript

Pradip Dhakal

There are few stackoverflow answers, which may help you.

But in summary, It's same as like passing other normal props.

Here is the basic example which may help you to understand:

// button.tsx
interface IProps {
  children: React.FC | string;
  onClick: (e: any) => void;
  type: "button" | "reset";
}

const Button: React.FC<IProps> = (props) => {
  return (
    <button type={props.type} onClick={props.onClick}>
      {props.children}
    </button>
  );
};

export default Button;
import Button from "./button";

export default function App() {
  const myFunction = (e: any) => {
    console.log("Event is: ", e);
  };

  return (
    <Button type="button" onClick={myFunction}>
      Click Me
    </Button>
  );
}

In here, you can see, I've passed the myFunction to the Button component as a props, and call that method from the ButtonComponent itself, Whenever someone press the button, it execute the myFunction method from the parent App.tsx file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dropdown Value not passing from one component to the other - React JS

From Dev

Passing props from one class component to other in react js

From Dev

Passing function in React.js from one function component to another

From Dev

React typescript passing function as prop and calling from child component

From Dev

Passing string parameters into function from one component to another in react

From Dev

React TypeScript Passing a function to child component

From Dev

Passing a function as a prop to a Typescript React Functional Component

From Dev

Passing Generics to function component React Typescript

From Dev

Passing props from class component in react + typescript

From Dev

Proper way to inject into a base controller without passing from inheriting one

From Dev

React: When passing in a function to one instance of a component, why are all other instances effected?

From Dev

Passing state from one React Component to another

From Dev

Proper way to import functions from other file in TypeScript

From Dev

What is the proper way to create an event handler inside a React function component?

From Dev

Error Coming when passing props from one component to another and vice - versa in react using typescript

From Dev

React Typescript Router(5.1.8) data is not passing to other component as a query params

From Dev

How to access state value from one component to other function(not component) which is in separate file? React js

From Dev

React Component change from one type to the other

From Dev

Pass closeModal from one component to other with react

From Dev

Typescript function which returns one item from an array with proper type

From Dev

Getting toogleShow is not function when passing as props in react typescript functional component

From Javascript

Passing data from one function to another in React

From Dev

React call a function from other component

From Dev

Is there a better way to call a child function from a parent component in React, other than this?

From Dev

Correct way of passing data from one observable to other

From Dev

Fire function from one react component in another

From Dev

Passing state array from one component to another in React

From Dev

Passing state from one component to another with React language switch

From Dev

Passing class name down from one component to another in React

Related Related

  1. 1

    Dropdown Value not passing from one component to the other - React JS

  2. 2

    Passing props from one class component to other in react js

  3. 3

    Passing function in React.js from one function component to another

  4. 4

    React typescript passing function as prop and calling from child component

  5. 5

    Passing string parameters into function from one component to another in react

  6. 6

    React TypeScript Passing a function to child component

  7. 7

    Passing a function as a prop to a Typescript React Functional Component

  8. 8

    Passing Generics to function component React Typescript

  9. 9

    Passing props from class component in react + typescript

  10. 10

    Proper way to inject into a base controller without passing from inheriting one

  11. 11

    React: When passing in a function to one instance of a component, why are all other instances effected?

  12. 12

    Passing state from one React Component to another

  13. 13

    Proper way to import functions from other file in TypeScript

  14. 14

    What is the proper way to create an event handler inside a React function component?

  15. 15

    Error Coming when passing props from one component to another and vice - versa in react using typescript

  16. 16

    React Typescript Router(5.1.8) data is not passing to other component as a query params

  17. 17

    How to access state value from one component to other function(not component) which is in separate file? React js

  18. 18

    React Component change from one type to the other

  19. 19

    Pass closeModal from one component to other with react

  20. 20

    Typescript function which returns one item from an array with proper type

  21. 21

    Getting toogleShow is not function when passing as props in react typescript functional component

  22. 22

    Passing data from one function to another in React

  23. 23

    React call a function from other component

  24. 24

    Is there a better way to call a child function from a parent component in React, other than this?

  25. 25

    Correct way of passing data from one observable to other

  26. 26

    Fire function from one react component in another

  27. 27

    Passing state array from one component to another in React

  28. 28

    Passing state from one component to another with React language switch

  29. 29

    Passing class name down from one component to another in React

HotTag

Archive