单个Class

  1. //如果执行不了,把注释删掉即可。
  2. //首先定义Clock组件
  3. class Clock extends React.Component {
  4. //结构体 props是参数
  5. constructor(props) {
  6. super(props);//super关键字,它指代父类的实例(即父类的this对象)。子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。
  7. this.state = {date: new Date()};//this 方法来指定具体状态
  8. }
  9. //构建组件
  10. componentDidMount() {
  11. this.timerID = setInterval(
  12. () => this.tick(),
  13. 1000
  14. );
  15. }
  16. //销毁组件,使用完组件后记得销毁,不然会造成资源空间浪费
  17. componentWillUnmount() {
  18. clearInterval(this.timerID);
  19. }
  20. //tick方法设置具体日期
  21. tick() {
  22. this.setState({
  23. date: new Date()
  24. });
  25. }
  26. //前面已经定义过状态了,通过状态state去渲染 UIUI的整体架构设计是DOM实现,具体实现是state 渲染的。
  27. //以下是返回的html
  28. render() {
  29. return(
  30. //如果 hello greeting-left-left写成greeting-left,蓝色的部分会分成左右两部分,hello在左边,猫图片在右边
  31. <div className="greeting">
  32. <div className="greeting-left">
  33. <img
  34. src="http://placekitten.com/g/64/64" />
  35. <div className="greeting-left-left">hello</div>
  36. <p>{this.state.date.toLocaleString()}</p>
  37. </div>
  38. <div className="greeting-right">
  39. <p>hi</p>
  40. </div>
  41. </div>);
  42. }
  43. }
  44. //显示渲染的 UI
  45. ReactDOM.render( <Clock />,document.getElementById('root'));

CSS部分

  1. body{background: #a3d5d0;}
  2. .greeting{width:400px;
  3. height:200px;
  4. font-size:15px;
  5. border:1px solid red;}
  6. .greeting-left{width:49%;
  7. height:100%;
  8. border:1px solid blue;
  9. float: left}
  10. .greeting-right{width:49%;
  11. height:100%;
  12. border:1px solid green;
  13. float:left}

多个class

案例1

  1. // 第一步续:DOM上实现具体(另类实现)
  2. const appRoot = document.getElementById('app-root');
  3. const modalRoot = document.getElementById('modal-root');
  4. //定义隐藏层 modal组件
  5. class Modal extends React.Component {
  6. constructor(props) {super(props);
  7. this.el = document.createElement('div');}
  8. componentDidMount() {modalRoot.appendChild(this.el);}
  9. componentWillUnmount() {modalRoot.removeChild(this.el);}
  10. render() {// 创建一个 portal 去渲染子类
  11. return ReactDOM.createPortal(
  12. // Any valid React child: JSX, strings, arrays, etc.
  13. this.props.children,
  14. // A DOM element
  15. this.el,);}}
  16. //第二步:定义APP组件
  17. class App extends React.Component {
  18. constructor(props) {super(props);
  19. this.state = {showModal: false};}
  20. //=> 方法设置状态 this.handleShow = this.handleShow.bind(this);等价
  21. //设置显示处理的方法
  22. handleShow =()=> {this.setState({showModal: true});}
  23. //设置隐藏处理的方法
  24. handleHide =()=> {this.setState({showModal: false});}
  25. //通过设置的state 渲染 UI
  26. render() {//设置的隐藏层的html,第二步的Modal组件和这个设置相关联。
  27. const modal = this.state.showModal ? (
  28. <Modal>
  29. <div className="modal">
  30. <div>
  31. 191909010330
  32. </div>
  33. <br/> 194106220345
  34. <button onClick={this.handleHide}>Hide modal</button>
  35. </div>
  36. </Modal>
  37. ) : null;
  38. /返回显示层的html
  39. return (<div className="app">
  40. 3145926.<br/>
  41. <button onClick={this.handleShow}>Show modal</button>
  42. {modal}
  43. </div>);}}
  44. 第一步:显示UI
  45. ReactDOM.render(<App />, appRoot);

CSS部分

  1. body {background: #a3d5d0;}
  2. #modal-root {position: relative;
  3. z-index: 999;}
  4. .app {height: 10em;
  5. width: 10em;
  6. background: lightblue;
  7. overflow: hidden;}
  8. .modal {background-color: rgba(0,0,0,0.5);
  9. position: fixed;
  10. height: 100%;
  11. width: 100%;
  12. top: 0;
  13. left: 0;
  14. display: flex;
  15. align-items: center;
  16. justify-content: center;}

案例2
第二步是调用 Mouse 组件。是属于连接第一步和第二步

  1. //定义 Mouse 组件
  2. class Mouse extends React.Component {//结构体
  3. constructor(props) {
  4. super(props);//super关键字,它指代父类的实例(即父类的this对象)。子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。
  5. this.handleMouseMove = this.handleMouseMove.bind(this);
  6. this.state = { x: 0, y: 0 };//this方法指定状态}
  7. //设置状态
  8. handleMouseMove(event) {this.setState({
  9. x: event.clientX,
  10. y: event.clientY});}
  11. //通过状态state渲染UIUI整体框架设计是DOM设计,具体实现是state渲染的。 返回一个html
  12. render() {
  13. return (<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>{}
  14. <p>The current mouse position is ({this.state.x}, {this.state.y})</p></div>);}}
  15. //第二步 定义MouseTracker 组件 .<Mouse/>是调用 Mouse 组件
  16. class MouseTracker extends React.Component {
  17. render() {
  18. return (
  19. <div>
  20. <h1>Move the mouse around!</h1>
  21. <Mouse />
  22. </div>);}}
  23. //第一步显示UI
  24. ReactDOM.render(<MouseTracker />,document.getElementById('root'));

CSS 方法

  1. body {background: #a3d5d0;}
  2. #modal-root {position: relative;
  3. z-index: 999;}
  4. .app {height: 10em;
  5. width: 10em;
  6. background: lightblue;
  7. overflow: hidden;}
  8. .modal {background-color: rgba(0,0,0,0.5);
  9. position: fixed;
  10. height: 100%;
  11. width: 100%;
  12. top: 0;
  13. left: 0;
  14. display: flex;
  15. align-items: center;
  16. justify-content: center;}

单个function

  1. //定义时间函数,生成html组件
  2. function tick() {
  3. const element = (<div>
  4. <h1>Hello, world!</h1>
  5. <h2>It is {new Date().toLocaleTimeString()}.</h2>
  6. </div>);
  7. ReactDOM.render(element, document.getElementById('root'));//显示UI}
  8. //它调用ReactDOM.render()每秒钟setInterval()回调
  9. setInterval(tick, 1000);

多个function

  1. //定义 welcome函数
  2. function Welcome(crops){return <h1>Hello,{crops.name}</h1>;}
  3. // 定义App 函数并调用 welcome的方法
  4. function App(){
  5. return(<div>
  6. <Welcome name="saro"/>
  7. <Welcome name="l"/>
  8. <Welcome name="Z"/>
  9. </div>);}
  10. ReactDOM.render( <App/>,document.getElementById('root'));//显示UI

class+function结合版

  1. // 第一步续:DOM上显示UI
  2. const appRoot = document.getElementById('app-root');
  3. const modalRoot = document.getElementById('modal-root');
  4. //第三步 定义modal组件
  5. class Modal extends React.Component {
  6. constructor(props) {super(props);
  7. this.el = document.createElement('div');}
  8. componentDidMount() {modalRoot.appendChild(this.el);}
  9. componentWillUnmount() {modalRoot.removeChild(this.el);}
  10. render() {
  11. return ReactDOM.createPortal(
  12. this.props.children,
  13. this.el,);}}
  14. //第二步:定义父类组件
  15. class Parent extends React.Component {
  16. constructor(props) {super(props);
  17. this.state = {clicks: 0};}
  18. handleClick =()=> {
  19. this.setState(prevState => ({
  20. clicks: prevState.clicks + 1}));}
  21. render() {
  22. return (
  23. <div onClick={this.handleClick}>
  24. <p>Number of clicks: {this.state.clicks}</p>
  25. <p>
  26. Open up the browser DevTools
  27. to observe that the button
  28. is not a child of the div
  29. with the onClick handler.
  30. </p>
  31. <Modal>
  32. <Child />
  33. </Modal>
  34. </div>
  35. );
  36. }
  37. }
  38. //第四步:定义 child 函数
  39. function Child() {
  40. return (<div className="modal">
  41. <button>Click</button>
  42. </div>);}
  43. 第一步:显示父类组件
  44. ReactDOM.render(<Parent />, appRoot);

CSS方法

  1. body {background: #a3d5d0;}
  2. #modal-root {position: relative;
  3. z-index: 999;}
  4. .app {height: 10em;
  5. width: 10em;
  6. background: lightblue;
  7. overflow: hidden;}
  8. .modal {background-color: rgba(0,0,0,0.5);
  9. position: fixed;
  10. height: 100%;
  11. width: 100%;
  12. top: 0;
  13. left: 0;
  14. display: flex;
  15. align-items: center;
  16. justify-content: center;}

其他

  1. //定义名字和 element
  2. const name="Hilter"
  3. const element = <h1>Hello, {name}</h1>;
  4. ReactDOM.render( element, document.getElementById('root'));//显示UI

删除注释

如果程序不能执行,删除注释即可