How can I call a function from one component to another? React

Zeeshan Malik

I'm working in react.js. I've created a component Backend.jsx. I want it to work as a service (like in angular) where I can send API requests. I want to call methods of Backend in some other components.

I called this backend service in component and try to send data and get it in BackendService using props.

But obviously this will not work.

Here's my code

In Component:

This will be called after form submit.

handleLoginSubmit = (event) => {
    event.preventDefault();
    console.log(this.state.data);
    <BackendService onSendData = {this.state.data} />
}

In BackendService:

constructor(props) {
    super(props);
    this.state = {  }
    this.login(props)
}
login = (props) =>
{
    console.log('login', props);
};

Any suggestions how can I call this login method in component. Or any other suggestion to get component data in service.

Nagesh

You can try this:

1.Component.js

class Componet extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      data:"this state contain data"
    }

    this.backendServiceRef = React.createRef(); // Using this ref you can access method and state of backendService component.
  }

  render() {
    return (
      <div className="App">
        <button onClick={() => {

          this.backendServiceRef.current.login()  

       }}>click</button>

        <BackendService ref={this.backendServiceRef} onSendData = {this.state.data}></BackendService>
      </div>
    );
  }
}

export default Componet;  

2.BackendService.js

class BackendService extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    }
  }

  login = (props) => {
    alert("login call")
  };

  render() {
    return (
      <div>
        Backend service component
      </div>
    )
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I call a function in another CFC file from within a query of a function in one cfc file?

How can I call function from one csx file to another csx file in Azure?

React: How can i run a function from another Component with state Variables

how can i share function of one sibling to another sibling component

How to call a function from another file in a React component

How to call function from one component to another component with Button

How can I pass props from one component to another using Link and router in react

How do I call a mixin function from another component?

How would I call a function from another component in react native?

React - How can I pass API data from one component to another in a different js file?

I can not call an object from one function to another

How can I call one Angular function from another within the same service?

How I can one component to another component in react

how to call a function from another component in react

How do I call a function from another component

How to call a function from a component in another component?

Fire function from one react component in another

how to import a function from one component react to another component to render it?

How can I call a function from another function and how?

How can I emit from component and listen from another one?

React: Can i call a function component from a Class component on demand?

How can I call my array in one function to another function and display specific data from that array?

How can I call a function in a component from other components in React?

How to call function of one component from another in React

How can i call a modal component from another page

How can I re-render one component from another component in React?

How can I get the variable from other function to another component on React?

How can I call Child function from Parent Component in React

how can i pass data from a function to another file in react with calling that component or use props

TOP Ranking

HotTag

Archive