call a method of component from outside react

Solutions on MaxInterview for call a method of component from outside react by the best coders in the world

showing results for - "call a method of component from outside react"
Jannik
14 Feb 2017
1
2
3const MyComponent = ({myRef}) => {
4  const handleClick = () => alert('hello world')
5  myRef.current.handleClick = handleClick
6  return (<button onClick={handleClick}>Original Button</button>)
7}
8
9MyComponent.defaultProps = {
10  myRef: {current: {}}
11}
12
13const MyParentComponent = () => {
14  const myRef = React.useRef({})
15  return (
16    <>
17      <MyComponent 
18        myRef={myRef}
19      />
20      <button onClick={myRef.current.handleClick}>
21        Additional Button
22      </button>
23    </>
24  )
25}
26
Hannah
10 Jul 2016
1<!-- Parent.vue -->
2<template>
3  <ChildComponent ref="child" />
4</template>
5
6// Somewhere in Parent.vue
7this.$refs.child.method();