Skip to content

createClass的支持

司徒正美 edited this page May 3, 2018 · 2 revisions

为了兼容老版本,anu还是乖乖支持这个老API(1.0.4+)

<!DOCTYPE html>
<html>
  <head>
      <meta charset="utf-8">
    <script src="./dist/React.js"></script>
    <script src="https://cdn.bootcss.com/jquery/1.12.3/jquery.js"></script>

    <script src="./lib/babel.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
var RepoList = React.createClass({
  getInitialState: function() {
    return {
      loading: true,
      error: null,
      data: null
    };
  },
  componentDidMount() {
    this.props.promise.then(
      value => this.setState({loading: false, data: value}),
      error => this.setState({loading: false, error: error}));
  },
  render: function() {
    if (this.state.loading) {
      return <span>Loading...</span>;
    }
    else if (this.state.error !== null) {
      return <span>Error: {this.state.error.message}</span>;
    }
    else {
      var repos = this.state.data.items;
      var repoList = repos.map(function (repo, index) {
        return (
          <li key={index}><a href={repo.html_url}>{repo.name}</a> ({repo.stargazers_count} stars) <br/> {repo.description}</li>
        );
      });
      return (
        <main>
          <h1>Most Popular JavaScript Projects in Github</h1>
          <ol>{repoList}</ol>
        </main>
      );
    }
  }
});
ReactDOM.render(
  <RepoList promise={$.getJSON('https://api.github.com/search/repositories?q=javascript&sort=stars')} />,
  document.getElementById('example')
);
    </script>
  </body>
</html>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
   <script type='text/javascript' src="./dist/React.js"></script>
   <script type='text/javascript' src="./lib/createClass.js"></script>
   <!-- <script type='text/javascript' src="./react.js"></script>
      <script type='text/javascript' src="./react-dom.js"></script>-->
    <script type='text/javascript' src="./lib/ReactTestUtils.js"></script>
    <script type='text/javascript' src="./lib/babel.js"></script>

</head>

<body>

    <pre>React 15 </pre>


    <div id='root' class="root">

    </div>
    <script type='text/babel'>
          var container = document.getElementById("root")
          var PropTypes = React.PropTypes
          if (!window.ReactDOM) {
              window.ReactDOM = React;
          }
          var expect = function(a) {
              return {
                  toBe: function(b) {
                      console.log(a, 'vs\n', b, a === b);
                  },
                  toEqual(b) {
                      console.log(a, 'vs\n', b, a + '' === b + '');
                  },
                  toMatch(reg){
                    console.log(a, 'vs', reg, reg.test(a)); 
                  },
                  toWarnDev(b){
                    throw b
                  }
              };
         }; 
 var InputControlES5 = createReactClass({
  
    defaultProps: { //组件默认的props对象
        initialValue: ''
    },
    // 设置 initial state
    getInitialState: function() {//组件相关的状态对象
        return {
            text: this.props.initialValue || 'placeholder'
        };
    },
    handleChange: function(event) {
        this.setState({ //this represents react component instance
            text: event.target.value
        });
    },
    render: function() {
        return (
            <div>
                Type something:
                <input onChange={this.handleChange} value={this.state.text} />
                <p>{this.state.text}</p>
            </div>
        );
    }
});
var a = ReactDOM.render(<InputControlES5 />, container)
console.log(a)
    </script>


</html>