Skip to content

render props2

司徒正美 edited this page Sep 13, 2018 · 1 revision
<!DOCTYPE html>
<html>

<head>
  <title>example</title>
  <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/ReactPropTypes.js"></script>

  <script type='text/javascript' src="./lib/babel.js"></script>
  <!-- <script type='text/javascript' src="./react.js"></script>
    <script type='text/javascript' src="./react-dom.js"></script>-->
</head>

<body>

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

  </div>
  <script type='text/babel'>
        
        var container = document.getElementById('root');
          var div = container;
          if (!window.ReactDOM) {
              window.ReactDOM = React;
		  }
		
	 
 class MouseTracker extends React.Component {
  constructor(props) {
    super(props);
    this.handleMouseMove = this.handleMouseMove.bind(this);
    this.state = { x: 0, y: 0 };
  }

  handleMouseMove(event) {
    this.setState({
      x: event.clientX,
      y: event.clientY
    });
  }

  render() {
    return (
      <div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
        <h1>Move the mouse around!</h1>
        <p>The current mouse position is ({this.state.x}, {this.state.y})</p>
        <p>{this.props.render(this.state)}</p>
      </div>
    );
  }
}
class Cat extends React.Component {
  render() {
    const mouse = this.props.mouse
    return ( <img src="https://www.baidu.com/img/baidu_jgylogo3.gif" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} />
    );
  }
}


    const instance = ReactDOM.render(<MouseTracker render={(state)=>{
      return <Cat mouse={state} />
    }}></MouseTracker>, container);
  
   </script>

</html>