Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React16的componentDidCatch #76

Closed
RubyLouvre opened this issue Nov 13, 2017 · 1 comment
Closed

React16的componentDidCatch #76

RubyLouvre opened this issue Nov 13, 2017 · 1 comment

Comments

@RubyLouvre
Copy link
Owner

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
  <title>树组件</title>

  <script type='text/javascript' src="./dist/React.js"></script>
  <script type='text/javascript' src="./lib/ReactTestUtils.js"></script>
  <script type='text/javascript' src="./lib/babel.js"></script>
    
</head>

<body>

    <div>测试</div>
    <div id='example'></div>
    <script type='text/babel'>
      var container = document.getElementById("example")
      var div = container
     // var PropTypes = React.PropTypes
      if(!window.ReactDOM){
        window.ReactDOM = window.React
       
      }
      var PropTypes = React.PropTypes
      var expect = function(a) {
          return {
              toBe: function(b) {
                  console.log(a, "\nvs\n",b, a === b)
              }
          }
      }
      var app;
      var count = 0;

      class App extends React.Component {
          render() {
              if (this.props.stage === 1) {
                  return <div><UnunmountableComponent /></div>;
              } else {
                  return null;
              }
          }
      }

      class UnunmountableComponent extends React.Component {
          componentWillUnmount() {
              app.setState({});
              count++;
              throw Error("always fails");
          }

          render() {
              return <div>Hello {this.props.name}</div>;
          }
      }

      var container = document.createElement("div");

      var setRef = ref => {
          if (ref) {
              app = ref;
          }
      };

   //   expect(function() {
     try{
          ReactDOM.render(<App ref={setRef} stage={1} />, container);
          ReactDOM.render(<App ref={setRef} stage={2} />, container);
    //  }).toThrow();
     }catch(e){
       console.log(e)
     }
      expect(count).toBe(1);
    </script>
    <pre>
     
    </pre>
</body>

</html>

image

@RubyLouvre
Copy link
Owner Author

function discontinue(vnode){
    if(!vnode){
        return; 
    }
    if(vnode.vtype > 1){
        captureError(vnode.stateNode, "componentWillUnmount",[]);
        vnode.stateNode._hasError = true;
    }
    discontinue(vnode.child);
    discontinue(vnode.sibling);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant