React 4of4 Flashcards
<p>Is it recommended to use CSS In JS technique in React?</p>
<p>React does not have any opinion about how styles are defined but if you are a beginner then good starting point is to define your styles in a separate *.css file as usual and refer to them using className. This functionality is not part of React but came from third-party libraries. But If you want to try a different approach(CSS-In-JS) then styled-components library is a good option.</p>
<p>Do I need to rewrite all my class components with hooks?</p>
<p>No. But you can try Hooks in a few components(or new components) without rewriting any existing code. Because there are no plans to remove classes in ReactJS.</p>
<p>How to fetch data with React Hooks?</p>
<p>The effect hook called useEffect is used to fetch the data with axios from the API and to set the data in the local state of the component with the state hook’s update function.
<br></br>
<br></br>Let's take an example in which it fetches list of react articles from the API
<br></br>
<br></br>import React, { useState, useEffect } from 'react';
<br></br>import axios from 'axios';
<br></br>
<br></br>function App() {
<br></br> const [data, setData] = useState({ hits: [] });
<br></br>
<br></br> useEffect(() => {
<br></br> (async () => {
<br></br> const result = await axios(
<br></br> 'http://hn.algolia.com/api/v1/search?query=react',
<br></br> );
<br></br>
<br></br> setData(result.data);
<br></br> })()
<br></br> }, []);
<br></br>
<br></br> return (
<br></br> </p>
<ul>
<br></br> {data.hits.map(item => (
<br></br> <li>
<br></br> <a>{item.title}</a>
<br></br> </li>
<br></br> ))}
<br></br> </ul>
<br></br> );
<br></br>}
<br></br>
<br></br>export default App;
<br></br>
<br></br>Remember we provided an empty array as second argument to the effect hook to avoid activating it on component updates but only on mounting of the component. i.e, It fetches only on component mount.
<p>Is Hooks cover all use cases for classes?</p>
<p>Hooks doesn't cover all use cases of classes but there is a plan to add them soon. Currently there are no Hook equivalents to the uncommon getSnapshotBeforeUpdate and componentDidCatch lifecycles yet.</p>
<p>What is the stable release for hooks support?</p>
<p>React includes a stable implementation of React Hooks in 16.8 release for below packages
<br></br>
<br></br>React DOM
<br></br>React DOM Server
<br></br>React Test Renderer
<br></br>React Shallow Renderer</p>
<p>Why do we use array destructuring (square brackets notation) in useState?</p>
<p>When we declare a state variable with useState, it returns a pair — an array with two items. The first item is the current value, and the second is a function that updates the value. Using [0] and [1] to access them is a bit confusing because they have a specific meaning. This is why we use array destructuring instead.
<br></br>
<br></br>For example, the array index access would look as follows:
<br></br>
<br></br> var userStateVariable = useState('userProfile'); // Returns an array pair
<br></br> var user = userStateVariable[0]; // Access first item
<br></br> var setUser = userStateVariable[1]; // Access second item
<br></br>Whereas with array destructuring the variables can be accessed as follows:
<br></br>
<br></br>const [user, setUser] = useState('userProfile');</p>
<p>What are the sources used for introducing hooks?</p>
<p>Hooks got the ideas from several different sources. Below are some of them,
<br></br>
<br></br>Previous experiments with functional APIs in the react-future repository
<br></br>Community experiments with render prop APIs such as Reactions Component
<br></br>State variables and state cells in DisplayScript.
<br></br>Subscriptions in Rx.
<br></br>Reducer components in ReasonReact.</p>
<p>How do you access imperative API of web components?</p>
<p>Web Components often expose an imperative API to implement its functions. You will need to use a ref to interact with the DOM node directly if you want to access imperative API of a web component. But if you are using third-party Web Components, the best solution is to write a React component that behaves as a wrapper for your Web Component.</p>
<p>What is formik?</p>
<p>Formik is a small react form library that helps you with the three major problems,
<br></br>
<br></br>Getting values in and out of form state
<br></br>Validation and error messages
<br></br>Handling form submission</p>
<p>What are typical middleware choices for handling asynchronous calls in Redux?</p>
<p>Some of the popular middleware choices for handling asynchronous calls in Redux eco system are Redux Thunk, Redux Promise, Redux Saga.</p>
<p>Do browsers understand JSX code?</p>
<p>No, browsers can't understand JSX code. You need a transpiler to convert your JSX to regular Javascript that browsers can understand. The most widely used transpiler right now is Babel.</p>
<p>Describe about data flow in react?</p>
<p>React implements one-way reactive data flow using props which reduce boilerplate and is easier to understand than traditional two-way data binding.</p>
<p>What is react scripts?</p>
<p>The react-scripts package is a set of scripts from the create-react-app starter pack which helps you kick off projects without configuring. The react-scripts start command sets up the development environment and starts a server, as well as hot module reloading.</p>
<p>What are the features of create react app?</p>
<p>Below are the list of some of the features provided by create react app.
<br></br>
<br></br>React, JSX, ES6, Typescript and Flow syntax support.
<br></br>Autoprefixed CSS
<br></br>CSS Reset/Normalize
<br></br>A live development server
<br></br>A fast interactive unit test runner with built-in support for coverage reporting
<br></br>A build script to bundle JS, CSS, and images for production, with hashes and sourcemaps
<br></br>An offline-first service worker and a web app manifest, meeting all the Progressive Web App criteria.</p>
<p>What is the purpose of renderToNodeStream method?</p>
<p>The ReactDOMServer#renderToNodeStream method is used to generate HTML on the server and send the markup down on the initial request for faster page loads. It also helps search engines to crawl your pages easily for SEO purposes. Note: Remember this method is not available in the browser but only server.</p>
<p>What is MobX?</p>
<p>MobX is a simple, scalable and battle tested state management solution for applying functional reactive programming (TFRP). For reactJs application, you need to install below packages,
<br></br>npm install mobx --save
<br></br>npm install mobx-react --save</p>
<p>What are the differences between Redux and MobX?</p>
<p>Below are the main differences between Redux and MobX,</p>

Should I learn ES6 before learning ReactJS?
No, you don’t have to learn es2015/es6 to learn react. But you may find many resources or React ecosystem uses ES6 extensively. Let’s see some of the frequently used ES6 features,
[1] Destructuring: To get props and use them in a component // in es 5 var someData = this.props.someData var dispatch = this.props.dispatch
// in es6 const { someData, dispatch } = this.props
[2] Spread operator: Helps in passing props down into a component // in es 5
// in es6
[3] Arrow functions: Makes compact syntax // es 5 var users = usersList.map(function (user) { return <li>{user.name}</li> }) // es 6 const users = usersList.map(user => <li>{user.name}</li>);
What is Concurrent Rendering?
The Concurrent rendering makes React apps to be more responsive by rendering component trees without blocking the main UI thread. It allows React to interrupt a long-running render to handle a high-priority event. i.e, When you enabled concurrent Mode, React will keep an eye on other tasks that need to be done, and if there’s something with a higher priority it will pause what it is currently rendering and let the other task finish first. You can enable this in two ways,
// 1. Part of an app by wrapping with ConcurrentMode
// 2. Whole app using createRoot ReactDOM.unstable_createRoot(domNode).render();
What is the difference between async mode and concurrent mode?
Both refers the same thing. Previously concurrent Mode being referred to as “Async Mode” by React team. The name has been changed to highlight React’s ability to perform work on different priority levels. So it avoids the confusion from other approaches to Async Rendering.
Can I use javascript urls in react16.9?
Yes, you can use javascript: URLs but it will log a warning in the console. Because URLs starting with javascript: are dangerous by including unsanitized output in a tag like <a> and create a security hole.</a>
const companyProfile = {
website: “javascript: alert(‘Your website is hacked’)”,
};
// It will log a warning
</a><a>More details</a>
Remember that the future versions will throw an error for javascript URLs.
What is the purpose of eslint plugin for hooks?
The ESLint plugin enforces rules of Hooks to avoid bugs. It assumes that any function starting with ”use” and a capital letter right after it is a Hook. In particular, the rule enforces that,
Calls to Hooks are either inside a PascalCase function (assumed to be a component) or another useSomething function (assumed to be a custom Hook).
Hooks are called in the same order on every render.
What is the difference between Imperative and Declarative in React?
Imagine a simple UI component, such as a “Like” button. When you tap it, it turns blue if it was previously grey, and grey if it was previously blue.
The imperative way of doing this would be:
if( user.likes() ) { if( hasBlue() ) { removeBlue(); addGrey(); } else { removeGrey(); addBlue(); } } Basically, you have to check what is currently on the screen and handle all the changes necessary to redraw it with the current state, including undoing the changes from the previous state. You can imagine how complex this could be in a real-world scenario.
In contrast, the declarative approach would be:
if( this.state.liked ) { return ; } else { return ; } Because the declarative approach separates concerns, this part of it only needs to handle how the UI should look in a sepecific state, and is therefore much simpler to understand.
What are the benefits of using typescript with reactjs?
Below are some of the benefits of using typescript with Reactjs,
[1] It is possible to use latest JavaScript features
[2] Use of interfaces for complex type definitions
[3] IDEs such as VS Code was made for TypeScript
[4] Avoid bugs with the ease of readability and Validation