You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -33,7 +31,7 @@ Below is a list of ReactJS interview questions and answers.
33
31
|20 | [How to create refs?](#how-to-create-refs)
34
32
|21 |[What are forward refs?](#what-are-forward-refs)|
35
33
|22 |[Which is preferred option with in callback refs and findDOMNode()?](#which-is-preferred-option-with-in-callback-refs-and-finddomnode)|
36
-
|23 |[Why are String Refs legacy?](#why-are-string-refs-legacy)|
34
+
|23 |[Why are String Refs considered legacy?](#why-are-string-refs-considered-legacy)|
37
35
|24 |[What is virtual DOM?](#what-is-virtual-dom)|
38
36
|25 |[How virtual DOM works?](#how-virtual-dom-works)|
39
37
|26 |[What is the difference between ShadowDOM and VirtualDOM?](#what-is-the-difference-between-shadowdom-and-virtualdom)|
@@ -232,6 +230,7 @@ Below is a list of ReactJS interview questions and answers.
232
230
|212|[How redux-form initialValues get updated from state?](#how-redux-form-initialvalues-get-updated-from-state)|
233
231
|213|[How react propTypes allow different types of propTypes for one prop?](#how-react-proptypes-allow-different-types-of-proptypes-for-one-prop)|
234
232
|214|[How to import an SVG as a React component](#how-to-import-an-svg-as-a-react-component)|
233
+
|215|[Why are inline ref callbacks or functions not recommended?](#why-are-inline-ref-callbacks-or-functions-not-recommended)|
235
234
236
235
## Core ReactJS
237
236
@@ -518,7 +517,8 @@ You can use either if statements or ternary expressions which are available from
518
517
There will be a warning in the console if the key is not present on list items.
519
518
520
519
19. ### What is the use of refs?
521
-
The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when we need direct access to DOM element or an instance of a component.
520
+
There are two approaches
521
+
1. This is a recently added approach. The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when we need direct access to DOM element or an instance of a component.
522
522
523
523
20. ### How to create refs?
524
524
Refs are created using React.createRef() method and attached to React elements via the ref attribute. In order to use refs throughout the component, just assign the ref to the instance property with in constructor.
@@ -533,27 +533,35 @@ class MyComponent extends React.Component {
533
533
}
534
534
}
535
535
```
536
+
2. You can also use ref callbacks approach regardless of React version. For example, the search bar component's input element accessed as follows,
536
537
```jsx
537
-
classUserFormextendsComponent {
538
-
handleSubmit= () => {
539
-
console.log("Input Value is: ", this.input.value)
540
-
}
541
-
render () {
542
-
return (
543
-
<form onSubmit={this.handleSubmit}>
544
-
<input
545
-
type='text'
546
-
ref={(input) =>this.input= input} />// Access DOM input in handle submit
547
-
<button type='submit'>Submit</button>
548
-
</form>
549
-
)
550
-
}
538
+
classSearchBarextendsComponent {
539
+
constructor(props) {
540
+
super(props);
541
+
this.txtSearch=null;
542
+
this.state= { term:'' };
543
+
this.setInputSearchRef=e=> {
544
+
this.txtSearch= e;
545
+
}
546
+
}
547
+
onInputChange(event) {
548
+
this.setState({ term:this.txtSearch.value });
549
+
}
550
+
render() {
551
+
return (
552
+
<input
553
+
value={this.state.term}
554
+
onChange={this.onInputChange.bind(this)}
555
+
ref={this.setInputSearchRef} />
556
+
);
557
+
}
551
558
}
552
559
```
553
560
We can also use it in functional components with the help of closures.
561
+
**Note**: You can also use inline ref callbacks even though it is not a recommended approach
554
562
555
563
21. ### What are forward refs?
556
-
Ref forwarding is a feature that lets some components take a ref they receive, and pass it further down to a child.
564
+
Ref forwarding is a feature that lets components take a ref they receive, and pass it further down to a child.
@@ -594,10 +602,26 @@ class MyComponent extends Component {
594
602
}
595
603
```
596
604
597
-
23. ### Why are String Refs legacy?
598
-
If you worked with React before, you might be familiar with an older API where the ref attribute is a string, like "textInput", and the DOM node is accessed as this.refs.textInput. We advise against it because string refs have below issues, are considered legacy, and are likely to be removed in one of the future releases.
599
-
1. It requires that React keeps track of currently rendering component (since it can't guess this). This makes React a bit slower.
600
-
2. It is not composable, i.e. if a library puts a ref on the passed child, the user can't put another ref on it. Callback refs are perfectly composable.
605
+
23. ### Why are String Refs considered legacy?
606
+
If you worked with React before, you might be familiar with an older API where the ref attribute is a string, like "textInput", and the DOM node is accessed as this.refs.textInput. We advise against it because string refs have below issues, are considered legacy(from 16.3.x), and are likely to be removed in one of the future releases.
607
+
1. It requires that React keeps track of currently rendering component (since it can't guess this keyword). This makes React a bit slower.
608
+
2. It doesn't work as most people would expect with the "render callback" pattern (e.g. <DataGrid renderRow={this.renderRow} />)
609
+
```
610
+
classMyComponentextendsComponent {
611
+
renderRow= (index) => {
612
+
// This won't work. Ref will get attached to DataTable rather than MyComponent:
613
+
return<input ref={'input-'+ index} />;
614
+
615
+
// This would work though! Callback refs are awesome.
3. It is not composable, i.e. if a library puts a ref on the passed child, the user can't put another ref on it. Callback refs are perfectly composable.
601
625
------------------------------
602
626
24. ### What is virtual DOM?
603
627
The virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the “real” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.
@@ -3158,4 +3182,50 @@ const App = () => (
3158
3182
);
3159
3183
```
3160
3184
**Note**: Don't forget the curly braces in the import!. This feature is available with react-scripts@2.0.0 and higher.
3185
+
---------------------------------
3186
+
215. ### Why are inline ref callbacks or functions not recommended?
3187
+
If the ref callback is defined as an inline function, it will get called twice during updates, first with null and then again with the DOM element. This is because a new instance of the function is created with each render, so React needs to clear the old ref and set up the new one.
3188
+
```jsx
3189
+
classUserFormextendsComponent {
3190
+
handleSubmit= () => {
3191
+
console.log("Input Value is: ", this.input.value)
3192
+
}
3193
+
3194
+
3195
+
render () {
3196
+
return (
3197
+
<form onSubmit={this.handleSubmit}>
3198
+
<input
3199
+
type='text'
3200
+
ref={(input) =>this.input= input} />// Access DOM input in handle submit
3201
+
<button type='submit'>Submit</button>
3202
+
</form>
3203
+
)
3204
+
}
3205
+
}
3206
+
```
3207
+
But our expectation is for the ref callback to get called once, when the component mounts. One quick fix is to use the ES7 class property syntax to define the function
3208
+
```jsx
3209
+
classUserFormextendsComponent {
3210
+
handleSubmit= () => {
3211
+
console.log("Input Value is: ", this.input.value)
3212
+
}
3213
+
3214
+
setSearchInput= (input) => {
3215
+
this.input= input
3216
+
}
3217
+
3218
+
render () {
3219
+
return (
3220
+
<form onSubmit={this.handleSubmit}>
3221
+
<input
3222
+
type='text'
3223
+
ref={this.setSearchInput} />// Access DOM input in handle submit
0 commit comments