skip to Main Content

I have one function
const getData=()=> { }
declared in dashboard component.

I want to call this function in another component called Testing

Previously I tried using props but getting error getData() is not a function.

And the getData() function is showing unused in dashboard component.

I have one parent component named ChatBot.

How can I call this getdata() function from dashboard component to testing component by using usestate or useeffect.

2

Answers


  1. Try moving your getData() function out of dashboard to separate lets say utility file, and from their import it in both dashboard and testing

    OR

    A better way would be to declare it in chatbot component, probably you did some mistake while implementing it.

    Login or Signup to reply.
  2. There are certain ways for achieving this :

    1. The basic one is to move your getData() function to the parent component i.e. ChatBot and pass it to the child component as prop. As you already said that you tried it earlier by passing that function as a prop but it didn’t work. So, I think you sent the function in a wrong way as a prop.
    2. You can also move the getData() function to another file like utility and export it and the import in all the components where you need that function and use it.
    3. You can use "UseReducer" i.e. Built-In react or you can use redux toolkit or similar library to make your function be available (centralized) for all components where you just need to use a function (dispatch) to call your function for performing that action(i.e. getData()).
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search