Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

pass values and functions from a Child component to a Parent using a ref

// Parent Component
const App = () => {
  const ref = useRef();

  return (
    <div>
      <ComponentWithButton ref={ref} />
      <button onClick={() => ref.current.increment()}>another button</button>
    </div>
  );
};

// Child Component
const ComponentWithButton = forwardRef((props, ref) => {
  useImperativeHandle(ref, () => ({increment}))

  const [count, setCount] = useState(0);
  const increment = () => setCount(count + 1);
  return (
    <div>
      <button onClick={increment}>click</button>
      <h2>Count: {count}</h2>
    </div>
  )
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript get element by class domlist undefined 
Javascript :: js to es6 convertor 
Javascript :: wakatime cli installation via npm 
Javascript :: fiffo in javascript 
Javascript :: node_modulesexpresslib outerindex.js:508 this.stack.push(layer); 
Javascript :: Form Data error (unable to decode value) characters specials 
Javascript :: how to get the total price of all product in cart using react 
Javascript :: godot get sibling node 
Javascript :: javascriot html eqaul to jquery 
Javascript :: js wrap a function pass parameters to function 
Javascript :: action creators in redux 
Javascript :: typeorm class validation 
Javascript :: react app environment variables undefined even when starts with REACT_APP_ 
Javascript :: response conditions 
Javascript :: serverless web app with react netlify 
Javascript :: react native update performance useReducer 
Javascript :: Example of AggregateError in es12 
Javascript :: Method definition shorthand in ES6 
Javascript :: add script tag change on every new page in angular 8 
Javascript :: Storing Values With Assignment Operators 
Javascript :: regular expression for twitter embedded tweets 
Javascript :: socket io import es6 
Javascript :: dollar sign brackets javascript 
Javascript :: return inner range recursive 
Javascript :: discord.js play song 
Javascript :: change frame rate javascript 
Javascript :: Refresh Mathjax formater 
Javascript :: javascript jquery json quiz2 
Javascript :: Webpack: How to compile, write on disk and serve static content (js/css/html/assets) using webpack-dev-server 
Javascript :: path error 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =