|
315 | 315 | |299| [How to add a bootstrap for a react application?](#how-to-add-a-bootstrap-for-a-react-application)|
|
316 | 316 | |300| [Can you list down top websites or applications using react as front end framework?](#can-you-list-down-top-websites-or-applications-using-react-as-front-end-framework)|
|
317 | 317 | |301| [Is it recommended to use CSS In JS technique in React?](#is-it-recommended-to-use-css-in-js-technique-in-react)|
|
| 318 | +|302| [Do I need to rewrite all my class components with hooks?](#do-i-need-to-rewrite-all-my-class-components-with-hooks)| |
| 319 | +|303| [How to fetch data with React Hooks?](#how-to-fetch-data-with-react-hooks)| |
| 320 | +|304| [Is Hooks cover all use cases for classes?](#is-hooks-cover-all-use-cases-for-classes)| |
318 | 321 |
|
319 | 322 | ## Core React
|
320 | 323 |
|
321 | 324 | 1. ### What is React?
|
322 | 325 |
|
323 | 326 | React is an **open-source frontend JavaScript library** which is used for building user interfaces especially for single page applications. It is used for handling view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook's News Feed in 2011 and on Instagram in 2012.
|
324 |
| - |
325 |
| - React 是一个开源的前端 JavaScript 框架,可用于构建用户界面特别是单页应用程序。它被用于作为网页和移动应用的视图层。React 由 Facebook |
326 |
| - 的工程师 Jordan Walke 创建。在 2011 年 React 应用首次被部署到 Facebook 的信息流上,之后于 2012 年被应用到 Instagram 中。 |
327 |
| - |
328 | 327 |
|
329 | 328 | 2. ### What are the major features of React?
|
330 | 329 |
|
|
335 | 334 | * Follows *Unidirectional** data flow or data binding.
|
336 | 335 | * Uses **reusable/composable** UI components to develop the view.
|
337 | 336 |
|
338 |
| - React 的主要特性有: |
339 |
| - * 考虑到真实的 DOM 操作比较昂贵,它使用 VirtualDOM 来替代真实的 DOM |
340 |
| - * 支持服务端渲染 |
341 |
| - * 遵从单向数据流或数据绑定 |
342 |
| - * 使用可重用/可组合的 UI 组件开发视图 |
343 |
| - |
344 | 337 | 3. ### What is JSX?
|
345 | 338 |
|
346 | 339 | *JSX* is a XML-like syntax extension to ECMAScript (the acronym stands for *JavaScript XML*). Basically it just provides syntactic sugar for the `React.createElement()` function, giving us expressiveness of JavaScript along with HTML like template syntax.
|
|
358 | 351 | }
|
359 | 352 | }
|
360 | 353 | ```
|
361 |
| - JSX 是一个对 ECMAScript 进行类 XML 的语法的扩展。它只是为 `React.createElement()` 函数提供语法糖,从而让在我们在 JavaScript 中, |
362 |
| - 使用类 HTML 模板的语法,进行页面描述。 |
363 |
| - |
364 |
| - 在以下示例中,在 `<h1>` 标签中的文本会以 JavaScript 函数的形式返回给渲染函数。 |
365 |
| - |
366 |
| - ```jsx harmony |
367 |
| - class App extends React.Component { |
368 |
| - render() { |
369 |
| - return( |
370 |
| - <div> |
371 |
| - <h1>{'Welcome to React world!'}</h1> |
372 |
| - </div> |
373 |
| - ) |
374 |
| - } |
375 |
| - } |
376 |
| - ``` |
377 |
| - |
378 | 354 |
|
379 | 355 | 4. ### What is the difference between Element and Component?
|
380 | 356 |
|
381 | 357 | An *Element* is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. *Elements* can contain other *Elements* in their props. Creating a React element is cheap. Once an element is created, it is never mutated.
|
382 | 358 |
|
383 |
| - 一个 Element 是一个普通的对象,以 DOM 节点或其他组件的方式描述你期望在屏幕上呈现的内容。 |
384 |
| - |
385 |
| - React 元素的对象表示如下: |
386 | 359 | The object representation of React Element would be as follows:
|
387 | 360 |
|
388 | 361 | ```javascript
|
|
394 | 367 | ```
|
395 | 368 |
|
396 | 369 | The above `React.createElement()` function returns an object:
|
397 |
| - 上面的 `React.createElement()` 函数会返回一个对象。 |
398 | 370 |
|
399 | 371 | ```
|
400 | 372 | {
|
|
407 | 379 | ```
|
408 | 380 |
|
409 | 381 | And finally it renders to the DOM using `ReactDOM.render()`:
|
410 |
| - 最终使用 `ReactDOM.render()` 方法渲染到 DOM: |
411 | 382 |
|
412 | 383 | ```html
|
413 | 384 | <div id='login-btn'>Login</div>
|
414 | 385 | ```
|
415 | 386 |
|
416 |
| - Whereas a **component** can be declared in several different ways. It can be a class with a `render()` method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns an JSX tree as the output: |
417 |
| - |
418 |
| - 然而一个组件可以以多种不同方式进行声明。它可以是一个含有 `render()` 方法的类。在简单的场景中,它能被定义成函数。在其它场景,它接收 props |
419 |
| - 作为输入,返回 JSX 树作为输出: |
| 387 | + Whereas a **component** can be declared in several different ways. It can be a class with a `render()` method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output: |
420 | 388 |
|
421 | 389 | ```javascript
|
422 | 390 | const Button = ({ onLogin }) =>
|
423 | 391 | <div id={'login-btn'} onClick={onLogin} />
|
424 | 392 | ```
|
425 | 393 |
|
426 |
| - Then JSX gets transpiled to `React.createElement()` function tree: |
427 |
| - |
428 |
| - 接着 JSX 被转译为 `React.createElement()` 函数树: |
| 394 | + Then JSX gets transpiled to a `React.createElement()` function tree: |
429 | 395 |
|
430 | 396 | ```javascript
|
431 | 397 | const Button = ({ onLogin }) => React.createElement(
|
|
438 | 404 | 5. ### How to create components in React?
|
439 | 405 |
|
440 | 406 | There are two possible ways to create a component.
|
441 |
| - 这里有两种可行方式来创建一个组件: |
442 | 407 |
|
443 | 408 | 1. **Function Components:** This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as first parameter and return React elements:
|
444 | 409 |
|
445 |
| - 1. **Function Components:** 这是创建组件最简单的方式 |
446 |
| - |
447 | 410 | ```jsx harmony
|
448 | 411 | function Greeting({ message }) {
|
449 |
| - return <h1>{`Hello, ${message}`}</h1> |
| 412 | + return <h1>{`Hello, ${message}`}</h1>
|
450 | 413 | }
|
451 | 414 | ```
|
452 | 415 |
|
|
1109 | 1072 |
|
1110 | 1073 | 45. ### Why React uses `className` over `class` attribute?
|
1111 | 1074 |
|
1112 |
| - `class` is a keyword in JavaSript, and JSX is an extension of JavaScript. That's the principal reason why React uses `className` instead of `class`. Pass a string as the `className` prop. |
| 1075 | + `class` is a keyword in JavaScript, and JSX is an extension of JavaScript. That's the principal reason why React uses `className` instead of `class`. Pass a string as the `className` prop. |
1113 | 1076 |
|
1114 | 1077 | ```jsx harmony
|
1115 | 1078 | render() {
|
|
3879 | 3842 |
|
3880 | 3843 | 217. ### What are HOC factory implementations?
|
3881 | 3844 | There are two main ways of implementing HOCs in React. 1. Props Proxy (PP) and 2. Inheritance Inversion (II). They follow different approaches for manipulating the *WrappedComponent*.
|
| 3845 | +
|
3882 | 3846 | **Props Proxy**
|
3883 | 3847 |
|
3884 | 3848 | In this approach, the render method of the HOC returns a React Element of the type of the WrappedComponent. We also pass through the props that the HOC receives, hence the name **Props Proxy**.
|
|
3894 | 3858 | }
|
3895 | 3859 | ```
|
3896 | 3860 | **Inheritance Inversion**
|
| 3861 | +
|
3897 | 3862 | In this approach, the returned HOC class (Enhancer) extends the WrappedComponent. It is called Inheritance Inversion because instead of the WrappedComponent extending some Enhancer class, it is passively extended by the Enhancer. In this way the relationship between them seems **inverse**.
|
3898 | 3863 |
|
3899 | 3864 | ```jsx
|
|
4173 | 4138 | The render() method is the only required method in a class component. i.e, All methods other than render method are optional for a class component.
|
4174 | 4139 | 239. ### What are the possible return types of render method?
|
4175 | 4140 | Below are the list of following types used and return from render method,
|
4176 |
| - 1. **React elements:** Elements that instruct React to render a DOM node. It includes html elements such as <div/> and user defined elements. |
| 4141 | + 1. **React elements:** Elements that instruct React to render a DOM node. It includes html elements such as `<div/>` and user defined elements. |
4177 | 4142 | 2. **Arrays and fragments:** Return multiple elements to render as Arrays and Fragments to wrap multiple elements
|
4178 | 4143 | 3. **Portals:** Render children into a different DOM subtree.
|
4179 | 4144 | 4. **String and numbers:** Render both Strings and Numbers as text nodes in the DOM
|
|
5064 | 5029 | 9. Netflix
|
5065 | 5030 | 10. PayPal
|
5066 | 5031 | 301. ### Is it recommended to use CSS In JS technique in React?
|
5067 |
| - 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. |
| 5032 | + 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. |
| 5033 | +302. ### Do I need to rewrite all my class components with hooks? |
| 5034 | + 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. |
| 5035 | +303. ### How to fetch data with React Hooks? |
| 5036 | + 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. |
| 5037 | + Let's take an example in which it fetches list of react articles from the API |
| 5038 | + ```javascript |
| 5039 | + import React, { useState, useEffect } from 'react'; |
| 5040 | + import axios from 'axios'; |
| 5041 | + |
| 5042 | + function App() { |
| 5043 | + const [data, setData] = useState({ hits: [] }); |
| 5044 | + |
| 5045 | + useEffect(async () => { |
| 5046 | + const result = await axios( |
| 5047 | + 'http://hn.algolia.com/api/v1/search?query=react', |
| 5048 | + ); |
| 5049 | + |
| 5050 | + setData(result.data); |
| 5051 | + }, []); |
| 5052 | + |
| 5053 | + return ( |
| 5054 | + <ul> |
| 5055 | + {data.hits.map(item => ( |
| 5056 | + <li key={item.objectID}> |
| 5057 | + <a href={item.url}>{item.title}</a> |
| 5058 | + </li> |
| 5059 | + ))} |
| 5060 | + </ul> |
| 5061 | + ); |
| 5062 | + } |
| 5063 | + |
| 5064 | + export default App; |
| 5065 | + ``` |
| 5066 | + Remember we provided an empty array as second argument to the effect hook to avoid activating it on component updates but only for the mounting of the component. i.e, It fetches only for component mount. |
| 5067 | +304. ### Is Hooks cover all use cases for classes? |
| 5068 | + 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. |
0 commit comments