专栏名称: 前端外刊评论
最新、最前沿的前端资讯,最有深入、最干前端相关的技术译文。
目录
相关文章推荐
简约小生活  ·  容易长肉的主食,不是米饭而是它,常吃等于喝油 ·  3 小时前  
简约小生活  ·  容易长肉的主食,不是米饭而是它,常吃等于喝油 ·  3 小时前  
龙视新闻联播  ·  回眸“十四五”|黑龙江:当好国家粮食安全压舱石 ·  11 小时前  
龙视新闻联播  ·  回眸“十四五”|黑龙江:当好国家粮食安全压舱石 ·  11 小时前  
Java基基  ·  SpringBoot + Minio ... ·  14 小时前  
Java基基  ·  SpringBoot + Minio ... ·  14 小时前  
前端大全  ·  React已死,Vue已凉,前端AI彻底称王! ·  2 天前  
51好读  ›  专栏  ›  前端外刊评论

React 16 新特性全解(上)

前端外刊评论  · 公众号  · 前端  · 2019-03-04 07:00

正文

请到「今天看啥」查看全文


}

  • }


  • class BuggyCounter extends React . Component {

  • constructor ( props ) {

  • super ( props );

  • this . state = { counter : 0 };

  • this . handleClick = this . handleClick . bind ( this );

  • }


  • componentWillMount () {

  • throw new Error ( 'I am crash' );

  • }


  • handleClick () {

  • this . setState (({ counter }) => ({

  • counter : counter + 1

  • }));

  • }


  • render () {

  • if ( this . state . counter === 5 ) {

  • // Simulate a JS error

  • throw new Error ( 'I crashed!' );

  • }

  • return

  • # {this.state.counter}

  • ;

  • }

  • }


  • function App () {

  • return (




  • This is an example of error boundaries in React 16.




  • Click on the numbers to increase the counters .



  • The counter is programmed to throw when it reaches 5. This simulates a JavaScript error in a component .



  • ---


  • These two counters are inside the same error boundary . If one crashes , the error boundary will replace both of them .


  • ---



  • );

  • }


  • ReactDOM . render (

  • ,

  • document . getElementById ( 'root' )

  • );

  • demo演示:

    可以看到加上Error Boundary之后,除了出错的组件,其他的地方都不受影响。 而且它很清晰的告诉我们是哪个组件发生了错误。

    注意事项:

    Error Boundary无法捕获下面的错误:

    1、事件函数里的错误

    1. class MyComponent extends React.Component {

    2. constructor(props) {

    3. super(props);

    4. this.state = { error: null };

    5. this.handleClick = this.handleClick.bind(this);

    6. }


    7. handleClick() {

    8. try {

    9. // Do something that could throw

    10. } catch (error) {

    11. this.setState({ error });

    12. }

    13. }


    14. render() {

    15. if (this.state.error) {

    16. return

    17. # Caught an error.


    18. }

    19. return

    20. Click Me


    21. }

    22. }

    上面的例子中,handleClick方法里面发生的错误,Error Boundary是捕获不道德。因为它不发生在渲染阶段,所以采用try/catch来捕获。

    2、异步代码(例如setTimeout 或 requestAnimationFrame 回调函数)

    1. class A extends React.Component {

    2. render() {

    3. // 此错误无法被捕获,渲染时组件正常返回 ``

    4. setTimeout(() => {

    5. throw new Error('error')

    6. }, 1000)

    7. return (


    8. )

    9. }

    10. }

    3、服务端渲染

    因为服务器渲染不支持Error Boundary

    4、Error Boundary自身抛出来的错误 (而不是其子组件)

    那这里还遗留一个问题?错误边界放在哪里。一般来说,有两个地方:

    1、可以放在顶层,告诉用户有东西出错。但是我个人不建议这样,这感觉失去了错误边界的意义。因为有一个组件出错了,其他正常的也没办法正常显示了

    2、包在子组件外面,保护其他应用不崩溃。

    三、react portal

    在介绍这个新特性之前,我们先来看看为什么需要portal。在没有portal之前,如果我们需要写一个Dialog组件,我们会这样写。

    1. ...


    2. { needDialog ? : null }

    问题:

    1、最终渲染产生的html存在于JSX产生的HTML在一起,这时候dialog 如果需要position:absolute 控制位置的话,需要保证dialog 往上没有position:relative 的干扰。

    2、层级关系不清晰,dialog实际是独立在app之外的。

    所以这时候Portal降临。

    Portal可以帮助我们在JSX中跟普通组件一样直接使用dialog, 但是又可以让dialog内容层级不在父组件内,而是显示在独立于原来app在外的同层级组件。

    如何使用:

    HTML:

    1. // 这里为我们定义Dialog想要放入的位置

    JS:

    1. // These two containers are siblings in the DOM

    2. const appRoot = document.getElementById('app-root');

    3. const modalRoot = document.getElementById('modal-root');


    4. // Let's create a Modal component that is an abstraction around

    5. // the portal API.

    6. class Modal







    请到「今天看啥」查看全文


    推荐文章
    点点星光  ·  爱你的人和玩你的人(说得真好)
    8 年前
    中央广电总台中国之声  ·  最高法说了,婚内出轨要付出更大的代价
    7 年前
    前程无忧51job  ·  月薪三万,撑不起孩子的暑假?
    7 年前