Skip to content

Commit 79fd9d5

Browse files
committed
React with external libraries integration questions
1 parent 0147e87 commit 79fd9d5

File tree

1 file changed

+173
-1
lines changed

1 file changed

+173
-1
lines changed

README.md

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ Below is a list of ReactJS interview questions and answers.
167167
|159| [What is Redux DevTools?](#what-is-redux-devtools)|
168168
|160| [What are the features of Redux DevTools?](#what-are-the-features-of-redux-devtools)|
169169
|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)|
170182

171183
### What is ReactJS?
172184

@@ -2447,4 +2459,164 @@ ReactDOM.render(<App />, document.getElementById('app'));
24472459
componentDidMount(){
24482460
this.nameInput.focus();
24492461
}
2450-
```
2462+
```
2463+
### 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,
2488+
```
2489+
// Wrong
2490+
this.setState({
2491+
counter: this.state.counter + this.props.increment,
2492+
});
2493+
```
2494+
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
2560+
```
2561+
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,Array.prototype.includes"></script>
2562+
2563+
```
2564+
In the above script we had to explicitly request the **Array.prototype.includes** feature as it is not included in the default feature set.
2565+
### How to use https instead of http in create-react-app?
2566+
You just need to use HTTPS=true configuration. You can edit your package.json scripts section as below
2567+
```
2568+
"scripts": {
2569+
"start": "set HTTPS=true&&react-scripts start",
2570+
...
2571+
}
2572+
```
2573+
or just run set HTTPS=true&&npm start
2574+
### How to avoid using relative path imports in create-react-app?
2575+
Create a file called .env in the project root and write the import path
2576+
```
2577+
NODE_PATH=src/app
2578+
```
2579+
After that restart the development server. Now you should be able to import anything inside **src/app* without relative paths.
2580+
### How to add google analytics for react-router?
2581+
You need to follow two steps
2582+
1. Keep your history object
2583+
```
2584+
var history = createBrowserHistory();
2585+
2586+
ReactDOM.render((
2587+
<Router history={history}>
2588+
[...]
2589+
```
2590+
2. Add a listener to record each page view
2591+
```
2592+
history.listen(function (location) {
2593+
window.ga('set', 'page', location.pathname + location.search);
2594+
window.ga('send', 'pageview', location.pathname + location.search);
2595+
});
2596+
```
2597+
### 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
2599+
```
2600+
componentDidMount() {
2601+
this.interval = setInterval(() => this.setState({ time: Date.now() }), 1000);
2602+
}
2603+
componentWillUnmount() {
2604+
clearInterval(this.interval);
2605+
}
2606+
```
2607+
### 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,
2617+
```
2618+
<div style={{
2619+
transform: 'rotate(90deg)',
2620+
WebkitTransform: 'rotate(90deg)'
2621+
}}>Hello World</div>
2622+
```

0 commit comments

Comments
 (0)