Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

call a method of component from outside react



const MyComponent = ({myRef}) => {
  const handleClick = () => alert('hello world')
  myRef.current.handleClick = handleClick
  return (<button onClick={handleClick}>Original Button</button>)
}

MyComponent.defaultProps = {
  myRef: {current: {}}
}

const MyParentComponent = () => {
  const myRef = React.useRef({})
  return (
    <>
      <MyComponent 
        myRef={myRef}
      />
      <button onClick={myRef.current.handleClick}>
        Additional Button
      </button>
    </>
  )
}
Comment

Calling a Method from Outside of the Component

<!-- Parent.vue -->
<template>
  <ChildComponent ref="child" />
</template>

// Somewhere in Parent.vue
this.$refs.child.method();
Comment

PREVIOUS NEXT
Code Example
Javascript :: signin with google widget floating automatically 
Javascript :: i need to add content-type accept form data using node.js in middelware 
Javascript :: react native undedined map 
Javascript :: javascript reassignment 
Javascript :: react-image-lightbox npm 
Javascript :: useSate object 
Javascript :: get single element typeorm 
Javascript :: array destructuring mdn 
Javascript :: progressbar jquery 3 ajax 
Javascript :: html detect shift tab 
Javascript :: js page head comment 
Javascript :: js check if tab switched 
Javascript :: moodle confirm box 
Javascript :: js datatables sort hidden columns 
Javascript :: Generar números aleatorios en Javascript entre un mínimo y un máximo 
Javascript :: angular dynamic script loading 
Javascript :: react get dynamic window sizes 
Javascript :: fetch file on server using jquery 
Javascript :: js window selection get new line 
Javascript :: javascript once per day 
Javascript :: convert to arrow functions 
Javascript :: e.addEventListener("input", function(){ e.value?n.innerText="Nama: "+e.value:n.innerText=""; }); 
Javascript :: set select2 value from local storage 
Javascript :: paypal react native 
Javascript :: screenfull angular example 
Javascript :: indonesia whatsapp formatter javascript 
Javascript :: array itarate 
Javascript :: glua how to call a hook 
Javascript :: javascript regex tester online 
Javascript :: show hide pseudo element jquery 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =