Skip to content

Commit 1bcc874

Browse files
committed
Add new questions and answers
1 parent ae9d496 commit 1bcc874

File tree

1 file changed

+94
-24
lines changed

1 file changed

+94
-24
lines changed

README.md

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
To encourage us and if you like the project then click star(💫) and also any PR contributions are appreciated
33
<hr/>
44

5-
<img src="images/logo.jpeg" width="600" height="300">
6-
7-
![](https://img.shields.io/github/stars/sudheerj/reactjs-interview-questions/editor.md.svg) ![](https://img.shields.io/github/forks/sudheerj/reactjs-interview-questions/editor.md.svg) ![
5+
<img src="images/logo.jpeg" width="600" height="250">
86

97
Below is a list of ReactJS interview questions and answers.
108
-------------------------------------------------------------------
@@ -33,7 +31,7 @@ Below is a list of ReactJS interview questions and answers.
3331
|20 | [How to create refs?](#how-to-create-refs)
3432
|21 | [What are forward refs?](#what-are-forward-refs)|
3533
|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)|
3735
|24 | [What is virtual DOM?](#what-is-virtual-dom)|
3836
|25 | [How virtual DOM works?](#how-virtual-dom-works)|
3937
|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.
232230
|212| [How redux-form initialValues get updated from state?](#how-redux-form-initialvalues-get-updated-from-state)|
233231
|213| [How react propTypes allow different types of propTypes for one prop?](#how-react-proptypes-allow-different-types-of-proptypes-for-one-prop)|
234232
|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)|
235234

236235
## Core ReactJS
237236

@@ -518,7 +517,8 @@ You can use either if statements or ternary expressions which are available from
518517
There will be a warning in the console if the key is not present on list items.
519518
520519
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.
522522
523523
20. ### How to create refs?
524524
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 {
533533
}
534534
}
535535
```
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,
536537
```jsx
537-
class UserForm extends Component {
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+
class SearchBar extends Component {
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+
}
551558
}
552559
```
553560
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
554562
555563
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.
557565
558566
```jsx
559567
const ButtonElement = React.forwardRef((props, ref) => (
@@ -594,10 +602,26 @@ class MyComponent extends Component {
594602
}
595603
```
596604
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+
class MyComponent extends Component {
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.
616+
return <input ref={input => this['input-' + index] = input} />;
617+
}
618+
619+
render() {
620+
return <DataTable data={this.props.data} renderRow={this.renderRow} />
621+
}
622+
}
623+
```
624+
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.
601625
------------------------------
602626
24. ### What is virtual DOM?
603627
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 = () => (
31583182
);
31593183
```
31603184
**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+
class UserForm extends Component {
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+
class UserForm extends Component {
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
3224+
<button type='submit'>Submit</button>
3225+
</form>
3226+
)
3227+
}
3228+
}
3229+
3230+
```
31613231

0 commit comments

Comments
 (0)