Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

multiple refs react

//Give multiple refs to a single JSX Component/element in React
import React, { useRef } from "react";

const Child = React.forwardRef((props, ref) => {
  const { ref1, ref2 } = ref.current;
  console.log(ref1, ref2); 
//ref1.current will point to foo <p> element and ref2.current will point to bar <p> element

  return (
    <>
      <p ref={ref1}>foo</p>
      <p ref={ref2}>bar</p>
    </>
  );
});

export default function App() {
  const ref1 = useRef();
  const ref2 = useRef();
  const ref = useRef({ ref1, ref2 });
  
  //ref.ref1.current will point to foo <p> element and ref.ref2.current will point to bar <p> element
  return <Child ref={ref} />;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Anonymous Functions with arguments in JavaScript 
Javascript :: property binding angular 
Javascript :: is javascript object oriented 
Javascript :: LRANGE in redis 
Javascript :: can i use splice in string of javascript 
Javascript :: angular.json 
Javascript :: debug bar laravel unninstall 
Javascript :: jquery accordion toggle close open 
Javascript :: electron ipcmain send 
Javascript :: window parent frames 
Javascript :: store fetch data in variable javascript 
Javascript :: how to debug node js file in webpack 
Javascript :: get full height of element javascript 
Javascript :: Nodemailer Google Passport Oauth Strategy 
Javascript :: set get variable in url 
Javascript :: js Destructuring arrays and objects 
Javascript :: create your own programming language in javascript 
Javascript :: node.js vm 
Javascript :: javascript advanced interview questions 
Javascript :: express nodejs 
Javascript :: difference between || and ?? in js 
Javascript :: timezone in react js 
Javascript :: react native time range picker 
Javascript :: var json = $.parseJSON(request.responseText); 
Javascript :: react infinte scroll 
Javascript :: how to move an element to the cursor in javascript 
Javascript :: discord js slash command 
Javascript :: module.exports with JavaScript 
Javascript :: push.js 
Javascript :: sequelize migration limit 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =