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
+173-1Lines changed: 173 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,6 +167,18 @@ Below is a list of ReactJS interview questions and answers.
167
167
|159|[What is Redux DevTools?](#what-is-redux-devtools)|
168
168
|160|[What are the features of Redux DevTools?](#what-are-the-features-of-redux-devtools)|
169
169
|161|[How to focus an input element on page load?](#how-to-focus-an-input-element-on-page-load)|
170
+
|162|[What are the possible ways to update object in state?](#what-are-the-possible-ways-to-update-object-in-state)|
171
+
|163|[Why function is preferred over object for setState?](#why-function-is-preferred-over-object-for-setstate)|
172
+
|164|[How can we find the version of React at runtime in the browser?](#how-can-we-find-the-version-of-react-at-runtime-in-the-browser)|
173
+
|165|[What is redux form?](#what-is-redux-form)|
174
+
|166|[What are the main features of redux form?](#what-are-the-main-features-of-redux-form)|
175
+
|167|[How to use TypeScript for create-react-app applications?](#how-to-use-typescript-for-create-react-app-applications)|
176
+
|168|[What are the approaches to include pollyfills in your create-react-app?](#what-are-the-approaches-to-include-pollyfills-in-your-create-react-app)|
177
+
|169|[How to use https instead of http in create-react-app?](#how-to-use-https-instead-of-http-in-create-react-app)|
178
+
|170|[How to avoid using relative path imports in create-react-app?](#how-to-avoid-using-relative-path-imports-in-create-react-app)|
179
+
|171|[How to add google analytics for react-router?](#how-to-add-google-analytics-for-react-router)|
180
+
|171|[How to update react component for every second?](#how-to-update-react-component-for-every-second)|
181
+
|172|[How do you apply vendor prefixes to inline styles in reactjs?](#how-do-you-apply-vendor-prefixes-to-inline-styles-in-reactjs)|
### What are the possible ways to update object in state?
2464
+
Below are the two ways of updating object in the state.
2465
+
1.**Using an object**
2466
+
First create a copy of an object then do the changes
2467
+
```
2468
+
let user = Object.assign({}, this.state.user); //creating copy of object
2469
+
user.age = 30; //updating value
2470
+
this.setState({user});
2471
+
```
2472
+
Instead of using Object.assign we can also write it like this:
2473
+
```
2474
+
let user = {...this.state.user};
2475
+
```
2476
+
2.**Using a function**
2477
+
```
2478
+
this.setState(prevState => ({
2479
+
user: {
2480
+
...prevState.user,
2481
+
age: 30
2482
+
}
2483
+
}))
2484
+
```
2485
+
### Why function is preferred over object for setState?
2486
+
React may batch multiple setState() calls into a single update for performance. Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
2487
+
For example, the below counter example will fail to update as expected,
The preferred approach is to use function rather object for setState call. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument.
2495
+
The correct approach is to use function for the counter example,
2496
+
```
2497
+
// Correct
2498
+
this.setState((prevState, props) => ({
2499
+
counter: prevState.counter + props.increment
2500
+
}));
2501
+
```
2502
+
### How can we find the version of React at runtime in the browser?
2503
+
You can use **React.version** to find the version. The example to find the react version as below
2504
+
```
2505
+
const REACT_VERSION = React.version;
2506
+
2507
+
ReactDOM.render(
2508
+
<div>React version: {REACT_VERSION}</div>,
2509
+
document.getElementById('app')
2510
+
);
2511
+
```
2512
+
### What is redux form?
2513
+
Redux Form works with React and Redux to enable a form in React to use Redux to store all of its state. Redux Form can be used with raw HTML5 inputs, but it also works with very well with common UI frameworks like Material UI, React Widgets and React Bootstrap.
2514
+
### What are the main features of redux form?
2515
+
Below are the major features of redux form?
2516
+
1. Field values persistence via Redux store
2517
+
2. Validation (sync/async) and submission
2518
+
3. Formatting, parsing and normalization of field values
2519
+
### How redux-form initialValues get updated from state?
2520
+
You need to add **enableReinitialize : true** setting as below.
2521
+
```
2522
+
let InitializeFromStateForm = reduxForm({
2523
+
form: 'initializeFromState',
2524
+
enableReinitialize : true // this is needed!!
2525
+
})(UserEdit)
2526
+
```
2527
+
If your initialValues prop gets updated, your form will update too.
2528
+
2529
+
### How to use TypeScript for create-react-app applications?
2530
+
When you create a new project called my-app then supply **--scripts-version** option as **react-scripts-ts**. react-scripts-ts is a set of adjustments to take the standard create-react-app project pipeline and bring TypeScript into the mix.
2531
+
Now the project layout should look like the following:
2532
+
```
2533
+
my-app/
2534
+
├─ .gitignore
2535
+
├─ images.d.ts
2536
+
├─ node_modules/
2537
+
├─ public/
2538
+
├─ src/
2539
+
│ └─ ...
2540
+
├─ package.json
2541
+
├─ tsconfig.json
2542
+
├─ tsconfig.prod.json
2543
+
├─ tsconfig.test.json
2544
+
└─ tslint.json
2545
+
```
2546
+
### What are the approaches to include pollyfills in your create-react-app?
2547
+
Below are the two way which can be used
2548
+
1.**Manual import from core-js**
2549
+
2550
+
Create a file called (something like) polyfills.js and import it into root index.js file. Run npm install core-js or yarn add core-js and import your specific required features, like
2551
+
```
2552
+
/* polyfills.js */
2553
+
2554
+
import 'core-js/fn/array/find';
2555
+
import 'core-js/fn/array/includes';
2556
+
import 'core-js/fn/number/is-nan';
2557
+
```
2558
+
2.**Using Polyfill service**
2559
+
Use the polyfill.io CDN to retrieve custom, browser-specific polyfills by adding this line to index.html
### How to update react component for every second?
2598
+
You need to use setInterval to trigger the change, but you also need to clear the timer when the component unmounts to prevent it leaving errors and leaking memory
### How do you apply vendor prefixes to inline styles in reactjs?
2608
+
React does not apply vendor prefixes automatically. In order to add vendor prefixes, name the vendor prefix as per the following pattern, and add it as a separate prop.
2609
+
```
2610
+
-vendor-specific-prop: 'value'
2611
+
```
2612
+
It becomes
2613
+
```
2614
+
VendorSpecificProp: 'value'
2615
+
```
2616
+
For example, the transform property need to be added for vendor specific browsers as below,
0 commit comments