当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 你要的 React 面试知识点,都在这了

你要的 React 面试知识点,都在这了

2019年06月01日  | 移动技术网IT编程  | 我要评论

摘要: 问题很详细,插图很好看。

fundebug经授权转载,版权归原作者所有。

react是流行的javascript框架之一,在2019年及以后将会更加流行。react于2013年首次发布,多年来广受欢迎。它是一个声明性的、基于组件的、用于构建用户界面的高效javascript库。

以下是面试前必须了解的话题。

  • 什么是声明式编程
  • 声明式编程 vs 命令式编程
  • 什么是函数式编程
  • 什么是组件设计模式
  • react 是什么
  • react 和 angular 有什么不同
  • 什么是虚拟dom及其工作原理
  • 什么是jsx
  • 组件和不同类型
  • props 和 state
  • 什么是 proptypes
  • 如何更新状态和不更新状态
  • 组件生命周期方法
  • 超越继承的组合
  • 如何在react中应用样式
  • 什么是redux及其工作原理
  • 什么是react路由器及其工作原理
  • 什么是错误边界
  • 什么是 fragments
  • 什么是传送门(portals)
  • 什么是 context
  • 什么是 hooks
  • 如何提高性能
  • 如何在重新加载页面时保留数据
  • 如何从react中调用api
  • 总结

什么是声明式编程

声明式编程是一种编程范式,它关注的是你要做什么,而不是如何做。它表达逻辑而不显式地定义步骤。这意味着我们需要根据逻辑的计算来声明要显示的组件。它没有描述控制流步骤。声明式编程的例子有html、sql等

html file

// html
<div>
  <p>declarative programming</p>
</div>

sql file

select * from studens where firstname = 'declarative';

声明式编程 vs 命令式编程

声明式编程的编写方式描述了应该做什么,而命令式编程描述了如何做。在声明式编程中,让编译器决定如何做事情。声明性程序很容易推理,因为代码本身描述了它在做什么。

下面是一个例子,数组中的每个元素都乘以 2,我们使用声明式map函数,让编译器来完成其余的工作,而使用命令式,需要编写所有的流程步骤。

const numbers = [1,2,3,4,5];

// 声明式
const doublewithdec = numbers.map(number => number * 2);

console.log(doublewithdec)

// 命令式
const doublewithimp = [];
for(let i=0; i<numbers.length; i++) {
    const numberdouble = numbers[i] * 2;
    doublewithimp.push(numberdouble)
}

console.log(doublewithimp)

什么是函数式编程

函数式编程是声明式编程的一部分。javascript中的函数是第一类公民,这意味着函数是数据,你可以像保存变量一样在应用程序中保存、检索和传递这些函数。

函数式编程有些核心的概念,如下:

  • 不可变性(immutability)
  • 纯函数(pure functions)
  • 数据转换(data transformations)
  • 高阶函数 (higher-order functions)
  • 递归
  • 组合

不可变性(immutability)

不可变性意味着不可改变。 在函数式编程中,你无法更改数据,也不能更改。 如果要改变或更改数据,则必须复制数据副本来更改。

例如,这是一个student对象和changename函数,如果要更改学生的名称,则需要先复制 student 对象,然后返回新对象。

在javascript中,函数参数是对实际数据的引用,你不应该使用 student.firstname =“testing11”,这会改变实际的student 对象,应该使用object.assign复制对象并返回新对象。

let student = {
    firstname: "testing",
    lastname: "testing",
    marks: 500
}

function changename(student) {
    // student.firstname = "testing11" //should not do it
    let copiedstudent = object.assign({}, student);
    copiedstudent.firstname = "testing11";
    return copiedstudent;
}

console.log(changename(student));

console.log(student);

纯函数

纯函数是始终接受一个或多个参数并计算参数并返回数据或函数的函数。 它没有副作用,例如设置全局状态,更改应用程序状态,它总是将参数视为不可变数据。

我想使用 appendaddress 的函数向student对象添加一个地址。 如果使用非纯函数,它没有参数,直接更改 student 对象来更改全局状态。

使用纯函数,它接受参数,基于参数计算,返回一个新对象而不修改参数。

let student = {
    firstname: "testing",
    lastname: "testing",
    marks: 500
}

// 非纯函数
function appendaddress() {
    student.address = {streetnumber:"0000", streetname: "first", city:"somecity"};
}

console.log(appendaddress());

// 纯函数
function appendaddress(student) {
    let copystudent = object.assign({}, student);
    copystudent.address = {streetnumber:"0000", streetname: "first", city:"somecity"};
    return copystudent;
}

console.log(appendaddress(student));

console.log(student);

数据转换

我们讲了很多关于不可变性的内容,如果数据是不可变的,我们如何改变数据。如上所述,我们总是生成原始数据的转换副本,而不是直接更改原始数据。

再介绍一些 javascript内置函数,当然还有很多其他的函数,这里有一些例子。所有这些函数都不改变现有的数据,而是返回新的数组或对象。

let cities = ["irving", "lowell", "houston"];

// we can get the comma separated list
console.log(cities.join(','))
// irving,lowell,houston

// if we want to get cities start with i
const citiesi = cities.filter(city => city[0] === "i");
console.log(citiesi)
// [ 'irving' ]

// if we want to capitalize all the cities
const citiesc = cities.map(city => city.touppercase());
console.log(citiesc)
// [ 'irving', 'lowell', 'houston' ]

高阶函数

高阶函数是将函数作为参数或返回函数的函数,或者有时它们都有。 这些高阶函数可以操纵其他函数。

array.map,array.filter和array.reduce是高阶函数,因为它们将函数作为参数。

const numbers = [10,20,40,50,60,70,80]

const out1 = numbers.map(num => num * 100);
console.log(out1);
// [ 1000, 2000, 4000, 5000, 6000, 7000, 8000 ]

const out2 = numbers.filter(num => num > 50);
console.log(out2);
// [ 60, 70, 80 ]

const out3 = numbers.reduce((out,num) => out + num);
console.log(out3);
// 330

下面是另一个名为ispersonold的高阶函数示例,该函数接受另外两个函数,分别是 messageisyoung

const isyoung = age => age < 25;

const message = msg => "he is "+ msg;

function ispersonold(age, isyoung, message) {
    const returnmessage = isyoung(age)?message("young"):message("old");
    return returnmessage;
}

// passing functions as an arguments
console.log(ispersonold(13,isyoung,message))
// he is young

递归

递归是一种函数在满足一定条件之前调用自身的技术。只要可能,最好使用递归而不是循环。你必须注意这一点,浏览器不能处理太多递归和抛出错误。

下面是一个演示递归的例子,在这个递归中,打印一个类似于楼梯的名称。我们也可以使用for循环,但只要可能,我们更喜欢递归。

function printmyname(name, count) {
    if(count <= name.length) {
        console.log(name.substring(0,count));
        printmyname(name, ++count);
    }
}

console.log(printmyname("bhargav", 1));

/*
b
bh
bha
bhar
bharg
bharga
bhargav
*/

// withotu recursion
var name = "bhargav"
var output = "";
for(let i=0; i<name.length; i++) {
    output = output + name[i];
    console.log(output);
}

组合

在react中,我们将功能划分为小型可重用的纯函数,我们必须将所有这些可重用的函数放在一起,最终使其成为产品。 将所有较小的函数组合成更大的函数,最终,得到一个应用程序,这称为组合

实现组合有许多不同方法。 我们从javascript中了解到的一种常见方法是链接。 链接是一种使用表示法调用前一个函数的返回值的函数的方法。

这是一个例子。 我们有一个name,如果firstnamelastname大于5个单词的大写字母,刚返回,并且打印名称的名称和长度。

const name = "bhargav bachina";

const output = name.split(" ")
    .filter(name => name.length > 5)
    .map(val => {
    val = val.touppercase();
    console.log("name:::::"+val);
    console.log("count::::"+val.length);
    return val;
});

console.log(output)
/*
name:::::bhargav
count::::7
name:::::bachina
count::::7
[ 'bhargav', 'bachina' ]
*/

在react中,我们使用了不同于链接的方法,因为如果有30个这样的函数,就很难进行链接。这里的目的是将所有更简单的函数组合起来生成一个更高阶的函数。

const name = compose(
    splitmyname,
    counteachname,
    comvertuppercase,
    returnname
)

console.log(name);

什么是 react

react是一个简单的javascript ui库,用于构建高效、快速的用户界面。它是一个轻量级库,因此很受欢迎。它遵循组件设计模式、声明式编程范式和函数式编程概念,以使前端应用程序更高效。它使用虚拟dom来有效地操作dom。它遵循从高阶组件到低阶组件的单向数据流。

react 与 angular 有何不同?

angular是一个成熟的mvc框架,带有很多特定的特性,比如服务、指令、模板、模块、解析器等等。react是一个非常轻量级的库,它只关注mvc的视图部分。

angular遵循两个方向的数据流,而react遵循从上到下的单向数据流。react在开发特性时给了开发人员很大的自由,例如,调用api的方式、路由等等。我们不需要包括路由器库,除非我们需要它在我们的项目。

什么是virtual dom及其工作原理

react 使用 virtual dom 来更新真正的 dom,从而提高效率和速度。 我们来详细讨论这些。

什么是virtual dom

浏览器遵循html指令来构造文档对象模型(dom)。当浏览器加载html并呈现用户界面时,html文档中的所有元素都变成dom元素。

dom是从根元素开始的元素层次结构。例如,看看下面的html。

<div>
    <div>
        <h1>this is heading</h1>
        <p>this is paragraph</p>
        <div>
            <p>this is just a paragraon</p>
        </div>
    </div>
    <div>
        <h1>this is heading</h1>
        <p>this is paragraph</p>
        <div>
            <p>this is just a paragraon</p>
        </div>
    </div>
    <div>
        <h1>this is heading</h1>
        <p>this is paragraph</p>
        <div>
            <p>this is just a paragraon</p>
        </div>
    </div>
</div>

当在浏览器中加载这个html时,所有这些html元素都被转换成dom元素,如下所示

当涉及到spa应用程序时,首次加载,并在本身中加载更新后的数据或另一个html。当用户浏览站点时,我们使用新内容更新相同的。每当dom发生更改时,浏览器都需要重新计算css、进行布局并重新绘制web页面。

react 使用 virtual dom 有效地重建 dom。 对于我们来说,这使得dom操作的一项非常复杂和耗时的任务变得更加容易。 react从开发人员那里抽象出所有这些,以便在virtual dom的帮助下构建高效的ui。

虚拟dom是如何工作的

虚拟dom只不过是真实 dom 的 javascript对象表示。 与更新真实 dom 相比,更新 javascript 对象更容易,更快捷。 考虑到这一点,让我们看看它是如何工作的。

react将整个dom副本保存为虚拟dom

每当有更新时,它都会维护两个虚拟dom,以比较之前的状态和当前状态,并确定哪些对象已被更改。 例如,段落文本更改为更改。

现在,它通过比较两个虚拟dom 差异,并将这些变化更新到实际dom

一旦真正的dom更新,它也会更新ui

什么是 jsx

jsx是javascript的语法扩展。它就像一个拥有javascript全部功能的模板语言。它生成react元素,这些元素将在dom中呈现。react建议在组件使用jsx。在jsx中,我们结合了javascript和html,并生成了可以在dom中呈现的react元素。

下面是jsx的一个例子。我们可以看到如何将javascript和html结合起来。如果html中包含任何动态变量,我们应该使用表达式{}

import react from 'react';

export const header = () => {

    const heading = 'todo app'

    return(
        <div style={{backgroundcolor:'orange'}}>
            <h1>{heading}</h1>
        </div>
    )
}

代码部署后可能存在的bug没法实时知道,事后为了解决这些bug,花了大量的时间进行log 调试,这边顺便给大家推荐一个好用的bug监控工具 fundebug

组件和不同类型

react 中一切都是组件。 我们通常将应用程序的整个逻辑分解为小的单个部分。 我们将每个单独的部分称为组件。 通常,组件是一个javascript函数,它接受输入,处理它并返回在ui中呈现的react元素。

在react中有不同类型的组件。让我们详细看看。

函数/无状态/展示组件

函数或无状态组件是一个纯函数,它可接受接受参数,并返回react元素。这些都是没有任何副作用的纯函数。这些组件没有状态或生命周期方法,这里有一个例子。

import react from 'react';
import jumbotron from 'react-bootstrap/jumbotron';

export const header = () => {
    return(
        <jumbotron style={{backgroundcolor:'orange'}}>
            <h1>todo app</h1>
        </jumbotron>
    )
}

类/有状态组件

类或有状态组件具有状态和生命周期方可能通过setstate()方法更改组件的状态。类组件是通过扩展react创建的。它在构造函数中初始化,也可能有子组件,这里有一个例子。

import react from 'react';
import '../app.css';
import { todoform } from './todoform';
import { todolist } from './todolist';

export class dashboard extends react.component {

  constructor(props){
    super(props);

    this.state = {

    }
  }
  
  render() {
    return (
      <div classname="dashboard"> 
          <todoform />
          <todolist />
      </div>
    );
  }
}

受控组件

受控组件是在 react 中处理输入表单的一种技术。表单元素通常维护它们自己的状态,而react则在组件的状态属性中维护状态。我们可以将两者结合起来控制输入表单。这称为受控组件。因此,在受控组件表单中,数据由react组件处理。

这里有一个例子。当用户在 todo 项中输入名称时,调用一个javascript函数handlechange捕捉每个输入的数据并将其放入状态,这样就在 handlesubmit中的使用数据。

import react from 'react';
import form from 'react-bootstrap/form';
import button from 'react-bootstrap/button';
import row from 'react-bootstrap/row';
import col from 'react-bootstrap/col';

export class todoform extends react.component {
    constructor(props) {
      super(props);
      this.state = {value: ''};
  
      this.handlechange = this.handlechange.bind(this);
      this.handlesubmit = this.handlesubmit.bind(this);
    }
  
    handlechange(event) {
      this.setstate({value: event.target.value});
    }
  
    handlesubmit(event) {
      alert('a name was submitted: ' + this.state.value);
      event.preventdefault();
    }
  
    render() {
      return (
          <div classname="todoform">
            <form>
                <form.group as={row} controlid="formhorizontalemail">
                    <form.label column sm={2}>
                    <span classname="item">item</span>
                    </form.label>
                    <col sm={5}>
                        <form.control type="text" placeholder="todo item" />
                    </col>
                    <col sm={5}>
                        <button variant="primary" type="submit">add</button>
                    </col>
                </form.group>
            </form>
         </div>
      );
    }
  }

非受控组件

大多数情况下,建议使用受控组件。有一种称为非受控组件的方法可以通过使用ref来处理表单数据。在非受控组件中,ref用于直接从dom访问表单值,而不是事件处理程序。

我们使用ref构建了相同的表单,而不是使用react状态。 我们使用react.createref() 定义ref并传递该输入表单并直接从handlesubmit方法中的dom访问表单值。

import react from 'react';
import form from 'react-bootstrap/form';
import button from 'react-bootstrap/button';
import row from 'react-bootstrap/row';
import col from 'react-bootstrap/col';

export class todoform extends react.component {
    constructor(props) {
      super(props);
      this.state = {value: ''};
      this.input = react.createref();
  
      this.handlesubmit = this.handlesubmit.bind(this);
    }
  
    handlesubmit(event) {
      alert('a name was submitted: ' + this.input.current.value);
      event.preventdefault();
    }
  
    render() {
      return (
          <div classname="todoform">
            <form>
                <form.group as={row} controlid="formhorizontalemail">
                    <form.label column sm={2}>
                    <span classname="item">item</span>
                    </form.label>
                    <col sm={5}>
                        <form.control type="text" placeholder="todo item" ref={this.input}/>
                    </col>
                    <col sm={5}>
                        <button variant="primary" onclick={this.handlesubmit} type="submit">add</button>
                    </col>
                </form.group>
            </form>
         </div>
      );
    }
  }

容器组件

容器组件是处理获取数据、订阅 redux 存储等的组件。它们包含展示组件和其他容器组件,但是里面从来没有html。

高阶组件

高阶组件是将组件作为参数并生成另一个组件的组件。 redux connect是高阶组件的示例。 这是一种用于生成可重用组件的强大技术。

props 和 state

props 是只读属性,传递给组件以呈现ui和状态,我们可以随时间更改组件的输出。

下面是一个类组件的示例,它在构造函数中定义了propsstate,每当使用this.setstate() 修改状态时,将再次调用 render( ) 函数来更改ui中组件的输出。

import react from 'react';
import '../app.css';

export class dashboard extends react.component {

  constructor(props){
    super(props);

    this.state = {
        name: "some name"
    }
  }

  render() {

    // reading state
    const name = this.state.name;

    //reading props
    const address = this.props.address;

    return (
      <div classname="dashboard"> 
          {name}
          {address}
      </div>
    );
  }
}

什么是proptypes

随着时间的推移,应用程序会变得越来越大,因此类型检查非常重要。proptypes为组件提供类型检查,并为其他开发人员提供很好的文档。如果react项目不使用 typescript,建议为组件添加 proptypes

如果组件没有收到任何 props,我们还可以为每个组件定义要显示的默认 props。这里有一个例子。userdisplay有三个 prop:nameaddressage,我们正在为它们定义默认的props 和 prop类型。

import react from 'react';
import proptypes from 'prop-types';

export const userdisplay = ({name, address, age}) => {

    userdisplay.defaultprops = {
        name: 'myname',
        age: 100,
        address: "0000 onestreet"
    };

    return (
        <>
            <div>
                <div class="label">name:</div>
                <div>{name}</div>
            </div>
            <div>
                <div class="label">address:</div>
                <div>{address}</div>
            </div>
            <div>
                <div class="label">age:</div>
                <div>{age}</div>
            </div>
        </>
    )
}

userdisplay.proptypes = {
    name: proptypes.string.isrequired,
    address: proptypes.objectof(proptypes.string),
    age: proptypes.number.isrequired
}

如何更新状态以及如何不更新

你不应该直接修改状态。可以在构造函数中定义状态值。直接使用状态不会触发重新渲染。react 使用this.setstate()时合并状态。

//  错误方式
this.state.name = "some name"
//  正确方式
this.setstate({name:"some name"})

使用this.setstate()的第二种形式总是更安全的,因为更新的props和状态是异步的。这里,我们根据这些 props 更新状态。

// 错误方式
this.setstate({
    timesvisited: this.state.timesvisited + this.props.count
})
// 正确方式
this.setstate((state, props) => {
    timesvisited: state.timesvisited + props.count
});

组件生命周期方法

组件在进入和离开dom时要经历一系列生命周期方法,下面是这些生命周期方法。

componentwillmount()

在渲染前调用,在客户端也在服务端,它只发生一次。

componentdidmount()

在第一次渲染后调用,只在客户端。之后组件已经生成了对应的dom结构,可以通过this.getdomnode()来进行访问。 如果你想和其他javascript框架一起使用,可以在这个方法中调用settimeout, setinterval或者发送ajax请求等操作(防止异部操作阻塞ui)。

componentwillreceiveprops()

在组件接收到一个新的 prop (更新后)时被调用。这个方法在初始化render时不会被调用。

shouldcomponentupdate()

返回一个布尔值。在组件接收到新的props或者state时被调用。在初始化时或者使用forceupdate时不被调用。 可以在你确认不需要更新组件时使用。

componentwillupdate()

在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。

componentdidupdate()

在组件完成更新后立即调用。在初始化时不会被调用。

componentwillunmount()

件从 dom 中移除的时候立刻被调用。

getderivedstatefromerror()

这个生命周期方法在errorboundary类中使用。实际上,如果使用这个生命周期方法,任何类都会变成errorboundary。这用于在组件树中出现错误时呈现回退ui,而不是在屏幕上显示一些奇怪的错误。

componentdidcatch()

这个生命周期方法在errorboundary类中使用。实际上,如果使用这个生命周期方法,任何类都会变成errorboundary。这用于在组件树中出现错误时记录错误。

超越继承的组合

在react中,我们总是使用组合而不是继承。我们已经在函数式编程部分讨论了什么是组合。这是一种结合简单的可重用函数来生成高阶组件的技术。下面是一个组合的例子,我们在 dashboard 组件中使用两个小组件todoformtodolist

import react from 'react';
import '../app.css';
import { todoform } from './todoform';
import { todolist } from './todolist';

export class dashboard extends react.component {

  render() {
    return (
      <div classname="dashboard"> 
          <todoform />
          <todolist />
      </div>
    );
  }
}

如何在react中应用样式

将样式应用于react组件有三种方法。

外部样式表

在此方法中,你可以将外部样式表导入到组件使用类中。 但是你应该使用classname而不是class来为react元素应用样式, 这里有一个例子。

import react from 'react';
import './app.css';
import { header } from './header/header';
import { footer } from './footer/footer';
import { dashboard } from './dashboard/dashboard';
import { userdisplay } from './userdisplay';

function app() {
  return (
    <div classname="app">
      <header />
      <dashboard />
      <userdisplay />
      <footer />
    </div>
  );
}

export default app;

内联样式

在这个方法中,我们可以直接将 props 传递给html元素,属性为style。这里有一个例子。这里需要注意的重要一点是,我们将javascript对象传递给style,这就是为什么我们使用 backgroundcolor 而不是css方法backbackground -color

import react from 'react';

export const header = () => {

    const heading = 'todo app'

    return(
        <div style={{backgroundcolor:'orange'}}>
            <h1>{heading}</h1>
        </div>
    )
}

定义样式对象并使用它

因为我们将javascript对象传递给style属性,所以我们可以在组件中定义一个style对象并使用它。下面是一个示例,你也可以将此对象作为 props 传递到组件树中。

import react from 'react';

const footerstyle = {
    width: '100%',
    backgroundcolor: 'green',
    padding: '50px',
    font: '30px',
    color: 'white',
    fontweight: 'bold'
}

export const footer = () => {
    return(
        <div style={footerstyle}>
            all rights reserved 2019
        </div>
    )
}

什么是redux及其工作原理

redux 是 react的一个状态管理库,它基于flux。 redux简化了react中的单向数据流。 redux将状态管理完全从react中抽象出来。

它是如何工作的

在react中,组件连接到 redux ,如果要访问 redux,需要派出一个包含 id和负载(payload) 的 action。action 中的 payload 是可选的,action 将其转发给 reducer。

reducer收到action时,通过 swithc...case 语法比较 actiontype。 匹配时,更新对应的内容返回新的 state

redux状态更改时,连接到redux的组件将接收新的状态作为props。当组件接收到这些props时,它将进入更新阶段并重新渲染 ui。

redux 循环细节

让我们详细看看整个redux 循环细节。

action: action 只是一个简单的json对象,type 和有payload作为键。type 是必须要有的,payload是可选的。下面是一个 action 的例子。

// action

{ 
  type:"send_email", 
  payload: data
};

action creators:这些是创建actions的函数,因此我们在派发action时不必在组件中手动编写每个 action。 以下是 action creator 的示例。

// action creator

export function sendeamil(data) {
    return { type:"send_email", payload: data};
}

reducers:reducers 是纯函数,它将 action和当前 state 作为参数,计算必要的逻辑并返回一个新r的state。 这些 reducers 没有任何副作用。 它不会改变 state 而是总是返回 state

export default function emailreducer(state = [], action){
 
  switch(action.type) {
      case "send_email":  return object.assign({}, state, {
       email: action.payload
      });
      default: return state;
  }
}

组件如何与 redux 进行连接

mapstatetoprops:此函数将state映射到 props 上,因此只要state发生变化,新 state 会重新映射到 props。 这是订阅store的方式。

mapdispatchtoprops:此函数用于将 action creators 绑定到你的props 。以便我们可以在第12行中使用this . props.actions.sendemail()来派发一个动作。

connectbindactioncreators来自 redux。 前者用于连接 store ,如第22行,后者用于将 action creators 绑定到你的 props ,如第20行。

// import connect
import { connect } from 'react-redux'
import { bindactioncreators } from 'redux'

// import action creators
import * as useractions from '../../../actions/useractions';

export class user extends react.component {
  
    handlesubmit() {
        // dispatch an action
        this.props.actions.sendemail(this.state.email);
    }
  
}

// you are mapping you state props
const mapstatetoprops = (state, ownprops) => ({user: state.user})
// you are binding your action creators to your props
const mapdispatchtoprops = (dispatch) => ({actions: bindactioncreators(useractions, dispatch)})

export default connect(mapstatetoprops, mapdispatchtoprops)(user);

什么是 react router dom 及其工作原理

react-router-dom是应用程序中路由的库。 react库中没有路由功能,需要单独安装react-router-dom

react-router-dom 提供两个路由器browserrouterhashroauter。前者基于rul的pathname段,后者基于hash段。

 前者:http://127.0.0.1:3000/article/num1

 后者:http://127.0.0.1:3000/#/article/num1(不一定是这样,但#是少不了的)

react-router-dom 组件

  • browserrouterhashrouter 是路由器。
  • route 用于路由匹配。
  • link 组件用于在应用程序中创建链接。 它将在html中渲染为锚标记。
  • navlink是突出显示当前活动链接的特殊链接。
  • switch 不是必需的,但在组合路由时很有用。
  • redirect 用于强制路由重定向

下面是组件中的linknavlinkredirect 的例子

// normal link
<link to="/gotoa">home</link>

// link which highlights currentlu active route with the given class name
<navlink to="/gotob" activeclassname="active">
  react
</navlink>

// you can redirect to this url
<redirect to="/gotoc" />

以下是 react router 组件的示例。 如果你查看下面的示例,我们将匹配路径并使用switchroute呈现相应的组件。

import react from 'react'
// import react router dom elements
import { switch, route, redirect } from 'react-router-dom'
import componenta from '../common/compa'
import componentb from '../common/compb'
import componentc from '../common/compc'
import componentd from '../common/compd'
import componente from '../common/compe'


const layout = ({ match }) => {
    return(
        <div classname="">
            <switch>
                <route exact path={`${match.path}/gotoa`} component={componenta} />
                <route path={`${match.path}/gotob`} component={componentb} />
                <route path={`${match.path}/gotoc`} component={componentc} />
                <route path={`${match.path}/gotod`} component={componentd} />
                <route path={`${match.path}/gotoe`} component={componente} />
            </switch>
        </div>
    )}

export default layout

什么是错误边界

在 react 中,我们通常有一个组件树。如果任何一个组件发生错误,它将破坏整个组件树。没有办法捕捉这些错误,我们可以用错误边界优雅地处理这些错误。

错误边界有两个作用

  • 如果发生错误,显示回退ui
  • 记录错误

下面是errorboundary类的一个例子。如果类实现了 getderivedstatefromerrorcomponentdidcatch 这两个生命周期方法的任何一下,,那么这个类就会成为errorboundary。前者返回{haserror: true}来呈现回退ui,后者用于记录错误。

import react from 'react'

export class errorboundary extends react.component {
    constructor(props) {
      super(props);
      this.state = { haserror: false };
    }
  
    static getderivedstatefromerror(error) {
      // update state so the next render will show the fallback ui.
      return { haserror: true };
    }
  
    componentdidcatch(error, info) {
      // you can also log the error to an error reporting service
      console.log('error::::', error);
    }
  
    render() {
      if (this.state.haserror) {
        // you can render any custom fallback ui
        return <h1>oops!. we are looking into it.</h1>;
      }
  
      return this.props.children; 
    }
  }

以下是我们如何在其中一个组件中使用errorboundary。使用errorboundary类包裹 todoformtodolist。 如果这些组件中发生任何错误,我们会记录错误并显示回退ui。

import react from 'react';
import '../app.css';
import { todoform } from './todoform';
import { todolist } from './todolist';
import { errorboundary } from '../errorboundary';

export class dashboard extends react.component {

  render() {
    return (
      <div classname="dashboard"> 
        <errorboundary>
          <todoform />
          <todolist />
        </errorboundary>
      </div>
    );
  }
}

什么是 fragments

在react中,我们需要有一个父元素,同时从组件返回react元素。有时在dom中添加额外的节点会很烦人。使用 fragments,我们不需要在dom中添加额外的节点。我们只需要用 react.fragment 或才简写 <> 来包裹内容就行了。如下 所示:

 // without fragments   
return (
    <div>
       <compoenta />
       <compoentb />
       <compoentc />
    </div>
)

// with fragments   
  return (
    <react.fragment>
       <compoenta />
       <compoentb />
       <compoentc />
    </react.fragment>
  )

  // shorthand notation fragments   
  return (
    <>
       <compoenta />
       <compoentb />
       <compoentc />
    </>
  )

什么是传送门(portals)

默认情况下,所有子组件都在ui上呈现,具体取决于组件层次结构。portal 提供了一种将子节点渲染到存在于父组件以外的 dom 节点的优秀的方案。

这里有一个例子。默认情况下,父组件在dom层次结构中有子组件。

我们可以将 children 组件移出parent 组件并将其附加 idsomeid 的 dom 节点下。

首先,获取 id 为 someid,我们在constrcutorand中创建一个元素div,将child附加到componentdidmount中的someroot。 最后,我们在reactdom.createportal(this.props.childen),domnode的帮助下将子节点传递给该特定dom节点。

首先,先获取 id 为someid dom元素,接着在构造函数中创建一个元素div,在 componentdidmount方法中将 someroot 放到 div 中 。 最后,通过
reactdom.createportal(this.props.childen), domnode)children 传递到对应的节点下。

const someroot = document.getelementbyid('someid');

class modal extends react.component {
  constructor(props) {
    super(props);
    this.el = document.createelement('div');
  }

  componentdidmount() {
    someroot.appendchild(this.el);
  }

  componentwillunmount() {
    someroot.removechild(this.el);
  }

  render() {
    return reactdom.createportal(
      this.props.children,
      this.el,
    );
  }
}

什么是上下文

有时我们必须将props 传递给组件树,即使所有中间组件都不需要这些props 。上下文是一种传递props 的方法,而不用在每一层传递组件树。

什么是 hooks

hooks 是react版本16.8中的新功能。 请记住,我们不能在函数组件中使用state ,因为它们不是类组件。hooks 让我们在函数组件中可以使用state 和其他功能。

目前没有重大变化,我们不必放弃类组件。

hook 不会影响你对 react 概念的理解。 恰恰相反,hook 为已知的 react 概念提供了更直接的 api:props, state,context,refs 以及生命周期。稍后我们将看到,hook 还提供了一种更强大的方式来组合他们。

我们可以使用一些钩子,例如usestate,useeffect,usecontext,usereducer等。

下面是 hooks 的基本规则

  • hooks 应该在外层使用,不应该在循环,条件或嵌套函数中使用
  • hooks 应该只在函数组件中使用。

让我们看一个例子来理解 hooks。 这是一个函数组件,它采用props并在ui上显示这些props。 在usestate钩子的帮助下,我们将这个函数组件转换为有状态组件。 首先,我们在第5行定义状态,这相当于

constructor(props) {
 super(props);
 this.state = {
     name:'myname', age:10, address:'0000 one street'
 }
}

usestate返回两个项,一个是user,另一个是setuser函数。 user 是一个可以在没有 this关键字的情况下直接使用的对象,setuser是一个可以用来设置用户点击第21行按钮的状态的函数,该函数等效于以下内容。

this.setstate({name:'name changed'})
import react, { usestate } from "react";

export const userdisplay = ({ name, address, age }) => {
    const [user, setuser] = usestate({
        name: "myname",
        age: 10,
        address: "0000 onestreet"
    });

    return (
        <>
            <div>
                <div class="label">name:</div>
                <div>{user.name}</div>
            </div>
            <div>
                <div class="label">address:</div>
                <div>{user.address}</div>
            </div>
            <div>
                <div class="label">age:</div>
                <div>{user.age}</div>
            </div>
            <button onclick={() => setuser({ name: "name changed" })}>
                click me
            </button>
        </>
    );
};

如何提高性能

我们可以通过多种方式提高应用性能,以下这些比较重要:

  • 适当地使用shouldcomponentupdate生命周期方法。 它避免了子组件的不必要的渲染。 如果树中有100个组件,则不重新渲染整个组件树来提高应用程序性能。
  • 使用create-react-app来构建项目,这会创建整个项目结构,并进行大量优化。
  • 不可变性是提高性能的关键。不要对数据进行修改,而是始终在现有集合的基础上创建新的集合,以保持尽可能少的复制,从而提高性能。
  • 在显示列表或表格时始终使用 keys,这会让 react 的更新速度更快
  • 代码分离是将代码插入到单独的文件中,只加载模块或部分所需的文件的技术。

如何在重新加载页面时保留数据

单页应用程序首先在dom中加载,然后在用户浏览页面时加载内容,或者从同一中的后端api获取任何数据。

如果通过点击浏览器中的重新加载按钮重新加载页面,整个react应用程序将重新加载,我们将丢失应用程序的状态。 如何保留应用状态?

每当重新加载应用程序时,我们使用浏览器localstorage来保存应用程序的状态。我们将整个存储数据保存在localstorage中,每当有页面刷新或重新加载时,我们从localstorage加载状态。

如何在react进行api调用

我们使用redux-thunk在react中调用api。因为reduce是纯函数,所以没有副作用,比如调用api。

因此,我们必须使用redux-thunk从 action creators 那里进行 api 调用。action creator 派发一个action,将来自api的数据放入action 的 payload 中。reducers 接收我们在上面的redux循环中讨论的数据,其余的过程也是相同的。

redux-thunk是一个中间件。一旦它被引入到项目中,每次派发一个action时,都会通过thunk传递。如果它是一个函数,它只是等待函数处理并返回响应。如果它不是一个函数,它只是正常处理。

这里有一个例子。sendemailapi是从组件中调用的函数,它接受一个数据并返回一个函数,其中dispatch作为参数。我们使用redux-thunk调用api apiservice,并等待收到响应。一旦接收到响应,我们就使用payload 派发一个action

import apiservice from '../services/apiservice';

export function sendemail(data) {
    return { type:"send_email", payload: data };
}

export function sendemailapi(email) {
    return function(dispatch) {
        return apiservice.callapi(email).then(data => {
            dispatch(sendemail(data));
        });
    }
}

总结

要想有把握的面试,必须充分了解上述所有主题。 即使你目前正在使用react,理解这些概念也能增强你在职场中信心。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网