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>