react: Calling child component's method from parent component

meteors

I have a ScrollView in react native and it has many Views. I store the ref to view using the following code

cards.push(
  <Card
    ref={(ref) => {
      console.log(ref);
      this.cardRef[index] = ref;
      ref.testMethod();
    }} />
);

Card is a separate component which looks like this:

class Card extends Component {
  constructor(props) {
    super(props);

    this.testMethod = this.testMethod.bind(this);
  }

  testMethod() {
    console.log('this.is.test.method');
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>This.is.a.card</Text>
      </View>
    )
  }
}

However it says that testMethod is undefinded, cannot call ref.testMethod().

alek kowalczyk

I played with your code on jsfiddle, and it looks like the child method is called:

var Card = React.createClass({
  testMethod() {
    console.log('this.is.test.method');
  },
  render() {
    return (
      <h1>this is a card.</h1>
    )
  }
});

var Parent = React.createClass({
    render: function() {
        return <div><Card ref={(r) => {r.testMethod()}}/></div>;
    }
});

ReactDOM.render(
    <Parent />,
    document.getElementById('container')
);

https://jsfiddle.net/vq110d69/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a method on Parent Component from Child Component

From Dev

How to call child component's method from a parent component in React

From Dev

Calling a method of a parent component from child - React Native

From Dev

Render child component or call child component's method from parent component and vice versa[React Native]

From Dev

Calling a method in the parent component from a child component using blazor server

From Dev

Calling parent component method from child component written in Jsx Vue

From Dev

Angular 14 calling Child Component method from Parent Component

From Dev

How to access child component's child from parent component in react?

From Dev

Trigger method on parent component from child component - react

From Dev

Call a method pass from parent component to child component in React

From Dev

Calling parent method from child component not working with callback

From Dev

Angular 4 - Calling a parent method from a child component not working

From Javascript

React js change child component's state from parent component

From Dev

React JS : Calling Child component method within parent component (React with typescript)

From Dev

Update parent React component from child component

From Dev

Calling Parent Function in Child Component in React

From Dev

React: problem calling child function in parent component

From Dev

Calling parent functions from child component

From Dev

How to call a child component method from parent in React Native typescript

From Dev

Child Component in Parent Component is calling different function on click React

From Dev

React access parent's dom node from child component

From Javascript

React - Call parent method in child component

From Dev

Expose state and method of child Component in parent with React

From Dev

Calling a method from child component in ionic 2

From Dev

Calling a children method of a functional component from parent

From Dev

React child Component in parent Component

From Dev

React Native: How to change parent component's state from child component and re-render parent?

From Dev

how to call child component's method from parent component's event in Reactjs

From Dev

Call method from parent to child via component

Related Related

  1. 1

    Calling a method on Parent Component from Child Component

  2. 2

    How to call child component's method from a parent component in React

  3. 3

    Calling a method of a parent component from child - React Native

  4. 4

    Render child component or call child component's method from parent component and vice versa[React Native]

  5. 5

    Calling a method in the parent component from a child component using blazor server

  6. 6

    Calling parent component method from child component written in Jsx Vue

  7. 7

    Angular 14 calling Child Component method from Parent Component

  8. 8

    How to access child component's child from parent component in react?

  9. 9

    Trigger method on parent component from child component - react

  10. 10

    Call a method pass from parent component to child component in React

  11. 11

    Calling parent method from child component not working with callback

  12. 12

    Angular 4 - Calling a parent method from a child component not working

  13. 13

    React js change child component's state from parent component

  14. 14

    React JS : Calling Child component method within parent component (React with typescript)

  15. 15

    Update parent React component from child component

  16. 16

    Calling Parent Function in Child Component in React

  17. 17

    React: problem calling child function in parent component

  18. 18

    Calling parent functions from child component

  19. 19

    How to call a child component method from parent in React Native typescript

  20. 20

    Child Component in Parent Component is calling different function on click React

  21. 21

    React access parent's dom node from child component

  22. 22

    React - Call parent method in child component

  23. 23

    Expose state and method of child Component in parent with React

  24. 24

    Calling a method from child component in ionic 2

  25. 25

    Calling a children method of a functional component from parent

  26. 26

    React child Component in parent Component

  27. 27

    React Native: How to change parent component's state from child component and re-render parent?

  28. 28

    how to call child component's method from parent component's event in Reactjs

  29. 29

    Call method from parent to child via component

HotTag

Archive