I have two react components the child component uses Apollo client for GraphQL which wraps the child component as higher order component. I want to access the child class method using reference to execute child method in parent class but when I use ref I get Graphql object instead of react child component. My code sample looks like this.
Note:
this question is not a duplicate of
React js change child component's state from parent component
OR
Access child state of child from parent component in react
Because
They don't use GraphQL Apollo Client)
Parent Component
import React, {Component} from 'react';
import "./Child";
class Parent extends Component{
state={
};
executeSomeChildFunction = () =>{
/* console.log(this.myChild); */
this.myChild.childFunction();
};
render(){
return(
<div>
<button onclick={this.executeSomeChildFunction}>
Click To Execute Child Function
</button>
<Child ref={el=> this.myChild =el} />
</div>
);
};
}
export default Parent;
Child Component
import React, {Component} from 'react';
const myQuery = `query DriversQuery() {
drivers: {
edges{
node{
id
pk
firstName
lastName
}
}
}
}`;
class Child extends Component{
state={
};
childFunction = () => {
alert("I am a child function");
};
render(){
return(
<div>Replace this div with your code!</div>
);
};
}
export default gql(myQuery, {/*some options*/})(Child);
What I Get as Output
When I console.log(this.myChild);
I get a graphql object like this.
Expected Output
I want to get Child Element Reference. So that When I console.log(this.myChild);
I should get Child component object.