(1.1)react组件es5

一个函数就是一个组件, 这个函数需要有一个return()

  1. import React from 'react';
  2. export default function() {
  3. return(
  4. <div>
  5. <h4>es5组件</h4>
  6. <p>
  7. hello react
  8. </p>
  9. </div>
  10. );
  11. }

(1.2)react组件es6

  1. 需要提供constructor并执行super()
  2. 需要提供一个render方法 ```javascript import React from ‘react’;

class Demo extends React.Component{ constructor() { super(); }

  1. // 需要提供一个render方法
  2. render() {
  3. return (
  4. <div>
  5. <h4>es6 组件</h4>
  6. <p>
  7. hello react
  8. </p>
  9. </div>
  10. )
  11. }

}

export default Demo;

  1. <a name="6fRJ3"></a>
  2. ## (2)react静态页面
  3. ```javascript
  4. import React from 'react';
  5. export default function() {
  6. return(
  7. <div>
  8. <h4>静态页面</h4>
  9. <div className="box" style={{border:'1px solid',color:'red'}}>
  10. 阿斯顿发是的发送到发斯蒂芬阿瑟发斯蒂芬
  11. </div>
  12. </div>
  13. )
  14. }

(3)react生命周期

  1. import React, { Component } from "react";
  2. class Demo extends Component {
  3. constructor() {
  4. super();
  5. }
  6. render() {
  7. return <div>生命周期函数</div>
  8. }
  9. componentWillMount() {
  10. console.log("组件将要挂载");
  11. }
  12. componentDidMount() {
  13. console.log("组件已经挂载");
  14. }
  15. componentWillReceiveProps(newProps) {
  16. console.log("组件将接收属性数据");
  17. }
  18. shouldComponentUpdate(nextProps, nextState) {
  19. // 此生命周期用来优化性能
  20. return true;
  21. }
  22. componentWillUpdate(nextProps, nextState) {
  23. console.log("组件将要更新");
  24. }
  25. componentDidUpdate(prevProps, prevState) {
  26. console.log("组件已经更新完毕");
  27. }
  28. componentWillUnmount() {
  29. console.log("组件将要卸载");
  30. }
  31. }
  32. export default Demo;

(4)jsx语法和注释

  1. import React from 'react';
  2. export default function() {
  3. let person = {
  4. username:'法外狂徒',
  5. page: 100
  6. }
  7. /**
  8. * jsx语法
  9. * 1. 遇到<>,看做html来进行解析
  10. * 2. 遇到{},看做是js来进行解析
  11. * 3. 注释{/* */}
  12. */
  13. return(
  14. <div>
  15. <p>姓名: {person.username}</p>
  16. <p>年龄: {person.age}</p>
  17. </div>
  18. )
  19. }

(5.1)事件绑定

  1. import React from "react";
  2. class Demo extends React.Component {
  3. constructor() {
  4. super();
  5. // 让test函数的this永远指向Demo实例
  6. this.test = this.test.bind(this);
  7. }
  8. test() {
  9. alert("哈哈哈哈哈哈哈");
  10. }
  11. render() {
  12. return (
  13. <div>
  14. <button onClick={this.test}>点我啊</button>
  15. </div>
  16. );
  17. }
  18. }
  19. export default Demo;

(5.2)事件绑定

  1. import React from 'react';
  2. class Demo extends React.Component{
  3. constructor() {
  4. super();
  5. this.test = this.test.bind(this);
  6. }
  7. test(data) {
  8. console.log(data);
  9. }
  10. render() {
  11. return(
  12. <div>
  13. <button onClick={()=>{this.test('111')}}>张三</button>
  14. <button onClick={()=>{this.test('222')}}>李四</button>
  15. <button onClick={()=>{this.test('333')}}>王五</button>
  16. </div>
  17. )
  18. }
  19. }
  20. export default Demo;

(6)列表渲染

  1. import React from 'react';
  2. class Demo extends React.Component{
  3. constructor() {
  4. super();
  5. }
  6. render() {
  7. let list = [
  8. {username:'zs1',age:20,id:"01"},
  9. {username:'zs2',age:21,id:"02"},
  10. {username:'zs3',age:22,id:"03"},
  11. {username:'zs4',age:23,id:"04"},
  12. ]
  13. return(
  14. <ul>
  15. {
  16. list.map((item,index)=> {
  17. return (
  18. <li key={item.id}>
  19. {index+1}. <span>用户名: {item.username}</span> <span>年龄: {item.age}</span>
  20. </li>
  21. )
  22. })
  23. }
  24. </ul>
  25. )
  26. }
  27. }
  28. export default Demo;

(7)条件渲染

  1. import React from 'react';
  2. export default function() {
  3. let isLogin = true;
  4. return (
  5. <div>
  6. <h4>条件渲染</h4>
  7. { isLogin? <p>138****0000</p>:
  8. <p>还没登录,<button>立即登录</button></p>
  9. }
  10. </div>
  11. )
  12. }

(8)state和setState

  1. import React from 'react';
  2. class Demo extends React.Component{
  3. constructor() {
  4. super();
  5. this.state = {
  6. isLogin: false
  7. }
  8. this.login = this.login.bind(this);
  9. }
  10. login() {
  11. // 修改state里的状态
  12. this.setState({
  13. isLogin: true
  14. });
  15. }
  16. render() {
  17. console.log('执行render方法');
  18. let {isLogin} = this.state;
  19. return (
  20. <div>
  21. <h4>条件渲染</h4>
  22. { isLogin? <p>138****0000</p>:<p>还没登录,<button onClick={this.login}>立即登录</button></p> }
  23. </div>
  24. )
  25. }
  26. }
  27. export default Demo;

(8.2)state和性能优化

  1. 只要执行setState, render函数默认会执行
  2. 使用shouldUpdateComponent进行性能优化, 只有当状态发生改变的时候在执行render函数 ```javascript import React from ‘react’;

class Demo extends React.Component{ constructor() { super(); this.state = { show: false } this.change1 = this.change1.bind(this); this.change2 = this.change2.bind(this); }

  1. change1() {
  2. // 修改state里的状态
  3. this.setState({
  4. show: !this.state.show
  5. });
  6. }
  7. change2() {
  8. // 修改state里的状态
  9. this.setState({
  10. show: true
  11. });
  12. }
  13. /**
  14. * 优化性能
  15. * @param {*} nextProps 下一个属性(新属性), nextState
  16. * @param {*} nextState 下一个state(新的状态)
  17. * @returns
  18. */
  19. shouldComponentUpdate(nextProps, nextState) {
  20. // 新的状态和旧的状态的值相同时, 不重新渲染页面
  21. if (this.state.show === nextState.show) {
  22. // 返回false不重新渲染页面
  23. return false;
  24. } else {
  25. // 返回true重新渲染页面
  26. return true;
  27. }
  28. }
  29. render() {
  30. console.log('重新渲染')
  31. let {show} = this.state;
  32. return (
  33. <div>
  34. <h4>staterender</h4>
  35. <p>
  36. <button onClick={this.change1}>btn1</button>
  37. <button onClick={this.change2}>btn2</button>
  38. </p>
  39. {
  40. show? <div>
  41. <p>sdfasdfasdfasdfasdfasdfasdf</p>
  42. <p>sdfasdfasdfasdfasdfasdfasdf</p>
  43. <p>sdfasdfasdfasdfasdfasdfasdf</p>
  44. </div>:''
  45. }
  46. </div>
  47. )
  48. }

}

export default Demo;

  1. <a name="OlAdG"></a>
  2. ## (9.1)表单输入
  3. 通过setState来显示用户的输入
  4. ```javascript
  5. import React from 'react';
  6. class Demo extends React.Component{
  7. constructor() {
  8. super();
  9. this.state = {
  10. username: ''
  11. }
  12. this.changeUsername = this.changeUsername.bind(this);
  13. }
  14. changeUsername() {
  15. this.setState({
  16. username: event.target.value
  17. })
  18. }
  19. render() {
  20. return(
  21. <div>
  22. <h4>表单输入</h4>
  23. <input type="text" value={this.state.username} onChange={this.changeUsername}/>
  24. </div>
  25. )
  26. }
  27. }
  28. export default Demo;

(9.2)多个输入框

  1. import React from 'react';
  2. class Demo extends React.Component{
  3. constructor() {
  4. super();
  5. this.state = {
  6. username: '',
  7. password:''
  8. }
  9. this.change = this.change.bind(this);
  10. }
  11. change() {
  12. // 获取输入的dom节点
  13. let target = event.target;
  14. // input标签上定义的name的值
  15. let name = target.name;
  16. this.setState({
  17. [name]: target.value
  18. })
  19. }
  20. render() {
  21. return(
  22. <div>
  23. <h4>表单输入</h4>
  24. <input placeholder="用户名" type="text" name="username" value={this.state.username}
  25. onChange={this.change}/>
  26. <input placeholder="密码" type="text" name="password" value={this.state.password}
  27. onChange={this.change}/>
  28. </div>
  29. )
  30. }
  31. }
  32. export default Demo;

(10)获取真实的 DOM 节点

  1. import React from "react";
  2. class Demo extends React.Component {
  3. constructor() {
  4. super();
  5. this.handleClick = this.handleClick.bind(this);
  6. }
  7. handleClick() {
  8. // 让输入框获得焦点
  9. this.refs.inp.focus();
  10. }
  11. render() {
  12. return (
  13. <span onClick={this.handleClick} style={{background:'#fff',display:'inline-block',width:'100px',height:'30px',border:'1px solid'}}>
  14. <input ref="inp" type="text" style={{width:'80%', border: 'none', outline: 'none'}}/>
  15. </span>
  16. );
  17. }
  18. }
  19. export default Demo;

(11)父子组件通信

  1. // 父组件
  2. import React from 'react';
  3. import Son from './Son'
  4. class Father extends React.Component{
  5. constructor() {
  6. super();
  7. this.getMsg = this.getMsg.bind(this);
  8. }
  9. // 接收子组件传过来的数据
  10. getMsg(data) {
  11. console.log(data);
  12. }
  13. render() {
  14. return(
  15. <div>
  16. <h3>父组件</h3>
  17. <hr/>
  18. {/* 给子组件传递属性和方法 */}
  19. <Son msg="hello son" getMsg={this.getMsg}/>
  20. </div>
  21. )
  22. }
  23. }
  24. export default Father;
  25. // 子组件
  26. import React from 'react';
  27. class Son extends React.Component{
  28. constructor() {
  29. super();
  30. }
  31. render() {
  32. let {msg,getMsg} = this.props;
  33. return(
  34. <div>
  35. {msg}<br/>
  36. {/* 调用父组件传过来的方法,并传递数据 */}
  37. <button onClick={()=>{getMsg('哈哈哈哈哈')}}>发送消息</button>
  38. </div>
  39. )
  40. }
  41. }
  42. export default Son;