ReactJS Tutorial - 30 - Forwarding Refs

ReactJS Tutorial - 30 - Forwarding Refs

Codevolution

5 лет назад

249,314 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

SIVA SAI NAGA MOHAN REDDY BEERAM
SIVA SAI NAGA MOHAN REDDY BEERAM - 27.10.2023 17:13

One doubt sir. You said one point here that ref.current points to the native input element and not the FRInput component instance. what is an instance? where it is located?

Ответить
Ambareesh Vishwanath
Ambareesh Vishwanath - 21.03.2023 05:44

What if I have a multiple input element in child component and i have to pass multiple ref from parent to child to assign, in that case how can we pass multiple refs using forwadingRef function

Ответить
Donia El Fouly
Donia El Fouly - 12.03.2023 23:16

Thanks

Ответить
Sufiyan ali
Sufiyan ali - 25.12.2022 19:49

what else can be done with refs? instead of focus...

Ответить
Vignesh Gunasekaran
Vignesh Gunasekaran - 14.11.2022 22:14

Forwarding Refs: (- referencing childComponents and accessing child elements)
Automatically forwarding a ref to child components
In above method, instead of referencing a child component, here we will directly reference the child’s input element.
Steps:
1. create reference variable in parent
2. attach to child component, when calling it.
React.forwardRef()
This takes “component” as input. So entire function goes into the parameter.
use arrow function for the component.
Every functional component takes ‘props’ as parameter.
But, when component is passed as parameter to createRef method, it will take ‘ref’ as 2nd parameter.
Thus ref is being forwarded from parent to child element.
3. Forward the ref to child’s input element
4. call focus directly now, from parent ref and current.
5. nothing else in child, except 3rd step

Ответить
Nathan TCHUENTE
Nathan TCHUENTE - 23.10.2022 01:27

marvelous

Ответить
Syed Baji
Syed Baji - 21.09.2022 16:55

What happens if we have more than one refs ?

Ответить
Subhra Jyoti Lahiri
Subhra Jyoti Lahiri - 27.07.2022 20:59

Can we forward refs for Class type components?

Ответить
Varun Adepu
Varun Adepu - 20.07.2022 06:49

Please use only functional components
If you mix up with class component it is little bit confusing

Ответить
msqar
msqar - 27.04.2022 16:01

Super interesting. Thanks for the explanation. Super clear.

Ответить
ethanappleseed
ethanappleseed - 16.04.2022 01:42

refs cannot be passed as regular props. this is from the official react docs. "ref is not a prop. Like key, it’s handled differently by React. If you add a ref to a HOC (higher order component), the ref will refer to the outermost container component, not the wrapped component." this is why we need to use React.forwardRef()

Ответить
drozdek
drozdek - 16.02.2022 17:37

All good, but just cannot digest your accent. Sorry...

Ответить
Artifex70
Artifex70 - 31.01.2022 22:33

Thanks, man you saved my life and time. I was facing a problem and you helped me to fix it.

Ответить
Raghavendra Reddy
Raghavendra Reddy - 11.01.2022 18:34

What if there is more than one input how to handle it? Whether need to pass form ref and use it?

Ответить
Powerful-Manifestor-1111
Powerful-Manifestor-1111 - 10.01.2022 11:57

How to forward multiple refs?

Ответить
Rahul Sutreja
Rahul Sutreja - 09.11.2021 10:37

How I can forward multiple input ref using above example.?

Ответить
Vladislav Polovinkin
Vladislav Polovinkin - 21.10.2021 15:03

This video helped me a lot. Thank you

Ответить
IT Giants
IT Giants - 07.09.2021 18:38

Hello I am little bit confused so what I understand that if the relationship between functional component and class based component we cannot do as previous video because you didn't use forward refs and you were able to access a function inside the child component using the refs but in here you're doing all of this to be able to make relationship between functional and class based components am I right or I got the idea wrongly?

Ответить
Mayank Makwana
Mayank Makwana - 05.09.2021 14:03

Disliked it. long video stretched it and speaks tons of random gibberish. not to the point!

Ответить
Aditya Sid
Aditya Sid - 05.09.2021 07:58

Amazing Video

Ответить
Himbary
Himbary - 17.08.2021 07:51

Very clear ty

Ответить
LeoNerdOso
LeoNerdOso - 27.07.2021 15:59

why mixing functional and class components? is it necessary?

Ответить
Rishabh Agrawal
Rishabh Agrawal - 20.07.2021 07:18

Mind blowing explanation with great n clear voice 👌

Ответить
All ANIME World
All ANIME World - 17.07.2021 12:24

Isn't there something like forwardRef we can use for class Components

Ответить
All ANIME World
All ANIME World - 17.07.2021 08:40

Let suppose we have 5 inputs and we need to assign ref on every input tag how do we do that

Ответить
Ivan Mokhonko
Ivan Mokhonko - 09.07.2021 18:20

what if i have multiple refs, how can i handle this

Ответить
iLike toCode
iLike toCode - 02.06.2021 00:24

That's native input can you put it on a react component

Ответить
Sakhan Bhakak
Sakhan Bhakak - 24.05.2021 19:08

WHY YOU ARE NOT EXPLAINING US WHY YOU USE FUNCTIONAL COMPONENT AND CLASS COMPENT IN THE CERTAIN EXAMPLE? YOU ARE GOOD DONT TREAT THAT COMMENT BAD. AND WHEN TO USE FUNCTIONAL OR CLASS COMPONENT?

Ответить
Nanda Kumar
Nanda Kumar - 22.04.2021 23:11

So forwarding refs can be used when the child component is functional component ?

Ответить
abdul rehman
abdul rehman - 22.03.2021 13:17

can we use jquery for this purpose? or its not a good approach to use jquery with react??

Ответить
Dimitri Alexander Laaz Menoscal
Dimitri Alexander Laaz Menoscal - 22.03.2021 09:08

Can i access ref from class component using this.props.ref and get the same effect ??

Ответить
Vignesh M
Vignesh M - 22.03.2021 06:58

Why can't we directly pass the ref as props?

Ответить
Natnael Zewdie
Natnael Zewdie - 03.02.2021 03:01

This is a great material to learn, but I have got a question. Is it possible to use forwadRef with class components?

Ответить
Lux Groove
Lux Groove - 30.12.2020 19:00

how can we pass multiple refs into one component? And how could we change their name when being passed so they are not all just called ref?

Ответить
Shubham Chauhan
Shubham Chauhan - 14.12.2020 11:41

Can we not pass down the ref instance as prop to the child component and then the child can assign the instance to whichever element it wants?

Ответить
Parshuram Kharade
Parshuram Kharade - 05.12.2020 11:49

Nice vedio and clear concept ..if you add with class component also ..so we can differentiate with functional component

Ответить
Michael The Profit Dreamer McGee
Michael The Profit Dreamer McGee - 01.12.2020 22:33

Thank you this helped greatly. REALLY NICE!!!

Ответить
adeel asghar
adeel asghar - 30.11.2020 09:55

its work when use <input></input>
but when i use "reactstrap" it doesnot work. <Input />
please solve this problem

Ответить
Даниил Гончаров
Даниил Гончаров - 30.11.2020 00:44

finaly i got it, thank you

Ответить
W Lancer
W Lancer - 23.11.2020 17:55

Adds another layer in between parent and child what then? forwardRef() twice?

Ответить
Alberto Azinar
Alberto Azinar - 27.10.2020 14:06

thanks for the gr8 explanation

Ответить
ינון אלבז
ינון אלבז - 25.10.2020 03:00

I program in react and i don't know about this topics . Thanks 👍

Ответить
Antanas Andrulis
Antanas Andrulis - 20.10.2020 11:59

It's funny how each video in these series have less viewers and all these people screaming how they love you are gone. Awesome work 🙃

Ответить
nithin samudrala
nithin samudrala - 30.09.2020 20:09

How can I call a method of child component from parent using ref

Ответить
Mamadou Niakaté
Mamadou Niakaté - 21.08.2020 13:43

What if one sends different refs to the component from different calling of the component

Ответить
Munish Thakur
Munish Thakur - 17.08.2020 20:54

For anyone who is confused between the previous video and this, let me break it down for you.
'ref 'in React is basically used to access HTML nodes, in the previous we learned that refs can also be used to access components in React, in a similar way we did the HTML element.
In this video we referenced the Child Component as well, but then we used React.forwardRef() to forward the reference from the child component to the input element inside the component.
Thus we were directly able to access the focus() method of <input> tag directly in our Parent Component via this.inputRef.current.focus(). if we would'nt have forwarded the ref we could'nt have accessed it directly via the current object.

Ответить
Chetan pl
Chetan pl - 11.08.2020 16:20

Superb.

Ответить
mithun kumar
mithun kumar - 05.08.2020 15:54

most useful tutorial. it would be great if each video has link to code explained in it.

Ответить