Skip to main content
Stack Overflow for Teams is now Stack Internal: See how we’re powering the human intelligence layer of enterprise AI. Read more >
minor grammar fixes
Source Link
xcopy
  • 2.3k
  • 19
  • 24

I have two components:

  1. Parent component
  2. Child component

I was trying to call Child's method from Parent, I tried this way but couldn't get a result:

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call Child's method from Parent?

Note: Child and Parent components are in two different files.

I have two components:

  1. Parent component
  2. Child component

I was trying to call Child's method from Parent, I tried this way but couldn't get a result:

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call Child's method from Parent?

Note: Child and Parent components are in two different files

I have two components:

  1. Parent component
  2. Child component

I was trying to call Child's method from Parent, I tried this way but couldn't get a result:

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call Child's method from Parent?

Note: Child and Parent components are in two different files.

I have two components.:

  1. Parent component
  2. Child component

I was trying to call child'sChild's method from Parent, I tried this way but couldn't get a result:

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call child'sChild's method from the parent Parent?

Note: Child and Parent components are in two different files

I have two components.

  1. Parent component
  2. Child component

I was trying to call child's method from Parent, I tried this way but couldn't get a result

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call child's method from the parent ?

Note: Child and Parent components are in two different files

I have two components:

  1. Parent component
  2. Child component

I was trying to call Child's method from Parent, I tried this way but couldn't get a result:

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call Child's method from Parent?

Note: Child and Parent components are in two different files

Question Protected by Josh Crozier
fixes formatting and adds javascript tag
Source Link
lukeaus
  • 12.3k
  • 8
  • 54
  • 61

I have two components.

  1. Parent component
  2. Child component

I was trying to call child's method from Parent, I tried this way but couldn't get a result

    class Parent extends Component {
     render() {
      return (
        <Child /><Child>
        <button onClick={Child.getAlert()}>Click</button>
      );</Child>
     } );
    }
 
    }

class Child extends Component {
     getAlert() {
        alert('clicked');
     }
    
  render() {
      return (
        <h1 ref="hello">Hello</h1>
      );
     }
    }

Is there a way to call child's method from the parent ?

Note: Child and Parent components are in two different files

I have two components.

  1. Parent component
  2. Child component

I was trying to call child's method from Parent, I tried this way but couldn't get a result

    class Parent extends Component {
     render() {
      return (
        <Child />
        <button onClick={Child.getAlert()}>Click</button>
      );
     }
    }
 
    class Child extends Component {
     getAlert() {
        alert('clicked');
     }
     render() {
      return (
        <h1 ref="hello">Hello</h1>
      );
     }
    }

Is there a way to call child's method from the parent ?

Note: Child and Parent components are in two different files

I have two components.

  1. Parent component
  2. Child component

I was trying to call child's method from Parent, I tried this way but couldn't get a result

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
  
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call child's method from the parent ?

Note: Child and Parent components are in two different files

added 65 characters in body
Source Link
N8FURY
  • 10.1k
  • 3
  • 17
  • 14
Loading
Source Link
N8FURY
  • 10.1k
  • 3
  • 17
  • 14
Loading