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
Copy file name to clipboardExpand all lines: README.md
+278Lines changed: 278 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,6 +58,16 @@ Curated top React.js interview questions with high quality answers for acing you
58
58
| 48 |[What is reconciliation in React?](#what-is-reconciliation-in-react)|
59
59
| 49 |[What is React Suspense and what does it enable?](#what-is-react-suspense-and-what-does-it-enable)|
60
60
| 50 |[Explain what happens when the `useState` setter function is called in React](#explain-what-happens-when-the-usestate-setter-function-is-called-in-react)|
61
+
| 51 |[What is a switching component?](#what-is-a-switching-component)|
62
+
| 52 |[What are React Mixins?](#what-are-react-mixins)|
63
+
| 53 |[What are the pointer events supported in React?](#what-are-the-pointer-events-supported-in-react)|
64
+
| 54 |[Why should component names start with a capital letter?](#why-should-component-names-start-with-capital-letter)|
65
+
| 55 |[Are custom DOM attributes supported in React v16?](#are-custom-dom-attributes-supported-in-react-v16)|
66
+
| 56 |[How do you loop inside JSX?](#how-to-loop-inside-jsx)|
67
+
| 57 |[How do you access props within attribute quotes?](#how-do-you-access-props-in-attribute-quotes)|
68
+
| 58 |[What is a React PropType array with shape?](#what-is-react-proptype-array-with-shape)|
69
+
| 59 |[How do you conditionally apply class attributes?](#how-to-conditionally-apply-class-attributes)|
70
+
| 60 |[Why is ReactDOM separated from React?](#why-reactdom-is-separated-from-react)|
61
71
62
72
<!-- TABLE_OF_CONTENTS:END -->
63
73
@@ -1111,5 +1121,273 @@ Curated top React.js interview questions with high quality answers for acing you
1111
1121
<br>
1112
1122
<br>
1113
1123
1124
+
51. ### What is a switching component?
1125
+
1126
+
A _switching component_ is a component that renders one of many components. We need to use object to map prop values to components.
1127
+
1128
+
For example, a switching component to display different pages based on `page` prop:
1129
+
1130
+
```jsx harmony
1131
+
importHomePagefrom"./HomePage";
1132
+
importAboutPagefrom"./AboutPage";
1133
+
importServicesPagefrom"./ServicesPage";
1134
+
importContactPagefrom"./ContactPage";
1135
+
1136
+
constPAGES= {
1137
+
home: HomePage,
1138
+
about: AboutPage,
1139
+
services: ServicesPage,
1140
+
contact: ContactPage,
1141
+
};
1142
+
1143
+
constPage= (props) => {
1144
+
constHandler=PAGES[props.page] || ContactPage;
1145
+
1146
+
return<Handler {...props} />;
1147
+
};
1148
+
1149
+
// The keys of the PAGES object can be used in the prop types to catch dev-time errors.
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/what-is-a-switching-component?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1158
+
1159
+
[Back to top ↑](#table-of-contents)
1160
+
<br>
1161
+
<br>
1162
+
1163
+
52. ### What are React Mixins?
1164
+
1165
+
_Mixins_ are a way to totally separate components to have a common functionality. Mixins **should not be used** and can be replaced with _higher-order components_ or _decorators_.
1166
+
1167
+
One of the most commonly used mixins is `PureRenderMixin`. You might be using it in some components to prevent unnecessary re-renders when the props and state are shallowly equal to the previous props and state:
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/what-are-react-mixins?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1181
+
1182
+
[Back to top ↑](#table-of-contents)
1183
+
<br>
1184
+
<br>
1185
+
1186
+
53. ### What are the Pointer Events supported in React?
1187
+
1188
+
_Pointer Events_ provide a unified way of handling all input events. In the old days we had a mouse and respective event listeners to handle them but nowadays we have many devices which don't correlate to having a mouse, like phones with touch surface or pens. We need to remember that these events will only work in browsers that support the _Pointer Events_ specification.
1189
+
1190
+
The following event types are now available in _React DOM_:
1191
+
1192
+
1. `onPointerDown`
1193
+
2. `onPointerMove`
1194
+
3. `onPointerUp`
1195
+
4. `onPointerCancel`
1196
+
5. `onGotPointerCapture`
1197
+
6. `onLostPointerCapture`
1198
+
7. `onPointerEnter`
1199
+
8. `onPointerLeave`
1200
+
9. `onPointerOver`
1201
+
10. `onPointerOut`
1202
+
1203
+
<br>
1204
+
1205
+
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/what-are-the-point-events-supported-in-react?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1206
+
1207
+
[Back to top ↑](#table-of-contents)
1208
+
<br>
1209
+
<br>
1210
+
1211
+
54. ### Why should component names start with capital letter?
1212
+
1213
+
If you are rendering your component using JSX, the name of that component has to begin with a capital letter otherwise React will throw an error as an unrecognized tag. This convention is because only HTML elements and SVG tags can begin with a lowercase letter.
1214
+
1215
+
```jsx harmony
1216
+
function SomeComponent {
1217
+
// Code goes here
1218
+
}
1219
+
```
1220
+
1221
+
You can define function component whose name starts with lowercase letter, but when it's imported it should have a capital letter. Here lowercase is fine:
1222
+
1223
+
```jsx harmony
1224
+
function myComponent {
1225
+
render() {
1226
+
return<div />;
1227
+
}
1228
+
}
1229
+
1230
+
exportdefaultmyComponent;
1231
+
```
1232
+
1233
+
While when imported in another file it should start with capital letter:
1234
+
1235
+
```jsx harmony
1236
+
importMyComponentfrom"./myComponent";
1237
+
```
1238
+
1239
+
<br>
1240
+
1241
+
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/why-should-component-names-start-with-capital-letter?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1242
+
1243
+
[Back to top ↑](#table-of-contents)
1244
+
<br>
1245
+
<br>
1246
+
1247
+
55. ### Are custom DOM attributes supported in React v16?
1248
+
1249
+
Yes. In the past, React used to ignore unknown DOM attributes. If you wrote JSX with an attribute that React doesn't recognize, React would just skip it.
1250
+
1251
+
For example, let's take a look at the below attribute:
1252
+
1253
+
```jsx harmony
1254
+
<div mycustomattribute={"something"} />
1255
+
```
1256
+
1257
+
Would render an empty div to the DOM with React v15:
1258
+
1259
+
```html
1260
+
<div />
1261
+
```
1262
+
1263
+
In React v16 any unknown attributes will end up in the DOM:
1264
+
1265
+
```html
1266
+
<div mycustomattribute="something"/>
1267
+
```
1268
+
1269
+
This is useful for supplying browser-specific non-standard attributes, trying new DOM APIs, and integrating with opinionated third-party libraries.
1270
+
1271
+
<br>
1272
+
1273
+
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/are-custom-dom-attributes-supported-in-react-v16?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1274
+
1275
+
[Back to top ↑](#table-of-contents)
1276
+
<br>
1277
+
<br>
1278
+
1279
+
56. ### How to loop inside JSX?
1280
+
1281
+
You can simply use `Array.prototype.map` with ES6 _arrow function_ syntax.
1282
+
1283
+
For example, the `items` array of objects is mapped into an array of components:
This is because JSX tags are transpiled into _function calls_, and you can't use statements inside expressions. This may change thanks to `do` expressions which are _stage 1 proposal_.
1304
+
1305
+
<br>
1306
+
1307
+
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/how-to-loop-inside-jsx?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1308
+
1309
+
[Back to top ↑](#table-of-contents)
1310
+
<br>
1311
+
<br>
1312
+
1313
+
57. ### How do you access props in attribute quotes?
1314
+
1315
+
React (or JSX) doesn't support variable interpolation inside an attribute value. The below representation won't work:
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/how-do-you-access-props-in-attribute-quotes?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1336
+
1337
+
[Back to top ↑](#table-of-contents)
1338
+
<br>
1339
+
<br>
1340
+
1341
+
58. ### What is React PropType array with shape?
1342
+
1343
+
If you want to pass an array of objects to a component with a particular shape then use `React.PropTypes.shape()` as an argument to `React.PropTypes.arrayOf()`.
1344
+
1345
+
```javascript
1346
+
ReactComponent.propTypes= {
1347
+
arrayWithShape:React.PropTypes.arrayOf(
1348
+
React.PropTypes.shape({
1349
+
color:React.PropTypes.string.isRequired,
1350
+
fontSize:React.PropTypes.number.isRequired,
1351
+
})
1352
+
).isRequired,
1353
+
};
1354
+
```
1355
+
1356
+
<br>
1357
+
1358
+
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/what-is-react-prop-type-array-with-shape?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
1359
+
1360
+
[Back to top ↑](#table-of-contents)
1361
+
<br>
1362
+
<br>
1363
+
1364
+
60. ### How to conditionally apply class attributes?
1365
+
1366
+
You shouldn't use curly braces inside quotes because it is going to be evaluated as a string.
> Read the [detailed answer](https://www.greatfrontend.com/questions/quiz/how-to-conditionally-apply-class-attributes?framework=react&tab=quiz) on [GreatFrontEnd](https://www.greatfrontend.com?gnrs=github) which allows progress tracking, contains more code samples, and useful resources.
0 commit comments