react.16+

打印 上一主题 下一主题

主题 510|帖子 510|积分 1530

1、函数式组件

在vite脚手架中实验:
app.jsx:
  1. import { useState } from 'react'
  2. import reactLogo from './assets/react.svg'
  3. import viteLogo from '/vite.svg'
  4. import './App.css'
  5. function App() {
  6.   console.log(this)
  7.   return <h2>我是函数式组件</h2>
  8. }
  9. export default App
复制代码
main.tsx:
  1. import React from 'react'
  2. import ReactDOM from 'react-dom/client'
  3. import App from './App.tsx'
  4. import './index.css'
  5. ReactDOM.createRoot(document.getElementById('root')!).render(
  6.   <React.StrictMode>
  7.     <App />
  8.   </React.StrictMode>,
  9. )
复制代码
注意:
        1、这里没有this,因为babel编译后开启了模式
        2、渲染的组件必须要大写开头(虚拟dom转为真实的dom)
2、类式组件

1、类式组件必须通过react的Component继承
2、组件必须在类中的render方法中返回
  1. import { Component } from "react"
  2. //使用类组件的时候必须要继承react中的Component
  3. //类组件不写构造器,必须要render
  4. //render放在组件的原型对象上
  5. //this是指向组件实例对象,
  6. class MyClassCom extends Component
  7. {
  8.     render()
  9.     {
  10.         return(
  11.             <div>
  12.                 <h1>This is Class Component</h1>
  13.             </div>
  14.         )
  15.     }
  16. }
  17. export {MyClassCom}
复制代码
3、组件三大核心(都是在类组件中使用,props可以在函数组件中使用)

3.1、state


  1. import { Component } from "react";
  2. //使用类组件的时候必须要继承react中的Component
  3. //类组件可以不写构造器,必须要render
  4. //render放在组件的原型对象上
  5. //this是指向组件实例对象,
  6. class MyClassCom extends Component {
  7.     //构造器调用一次
  8.   constructor(props) {
  9.     super(props);
  10.     //初始化状态
  11.     this.state = {
  12.       name: "张三",
  13.       isHot: false,
  14.     };
  15.     //绑定this,这里其实是重写的,可以用其他名字,但是下面调用也要改名字
  16.    this.b = this.b.bind(this);
  17.   }
  18.   //调用1+n次,n次是响应式状态更新的次数
  19.   render() {
  20.     return (
  21.       <div>
  22.         <h1>今天{this.state.isHot ? "炎热" : "凉快"}</h1>
  23.         <button onClick={this.a}>点击</button>
  24.         <button onClick={this.b}>点击</button>
  25.       </div>
  26.     );
  27.   }
  28.   a = () => {
  29.     //这里能拿到this,是因为箭头函数绑定了this
  30.     console.log(this);
  31.     //修改状态,必须通过setState修改状态
  32.     this.setState({
  33.       isHot: !this.state.isHot,
  34.     });
  35.   };
  36.   b() {
  37.     //因为是直接调用的类方法,不是实例对象调用的,所以拿不到this
  38.     //类中的方法默认开启了局部严格模式,所以this指向undefined
  39.     console.log(this);
  40.     this.setState({
  41.       isHot: !this.state.isHot,
  42.     });
  43.   }
  44. }
  45. export { MyClassCom };
复制代码
简写方式:
  1. import { Component } from "react";
  2. class MyClassCom extends Component {
  3.   //类中可以直接定义属性
  4.   state = {
  5.     name: "张三",
  6.     isHot: false,
  7.   };
  8.   render() {
  9.     return (
  10.       <div>
  11.         <h1>今天{this.state.isHot ? "炎热" : "凉快"}</h1>
  12.         <button onClick={this.a}>点击</button>
  13.       </div>
  14.     );
  15.   }
  16.   //直接使用箭头函数(箭头函数可以修改this指向),避免了this指向修改,也就不用构造器了
  17.   a = () => {
  18.     this.setState({
  19.       isHot: !this.state.isHot,
  20.     });
  21.   };
  22. }
  23. export { MyClassCom };
复制代码
总结:
1、state是组件对象的重要属性,值是对象
2、组件被称为”状态机”,通过更新组件的state来更新对应页面表现(重新渲染页面-可以明白为相应式)
3、组件中的render方法中的this为组件实例对象
4、组件自界说方法中的this为undefined(通过强制绑定this,通过对象的build(),如果是类组件但是要使用构造器,也可以直接使用箭头函数(推荐直接使用箭头函数))
5、状态数据不能直接修改或者更新,要通过setState修改更新
3.2、props

3.2.1、基本使用

封装组件:
  1. import { Component } from "react";
  2. class Person extends Component<{ name: string,age:string,sex:string }> {
  3.   render() {
  4.     const {name, age , sex} = this.props;
  5.     return (
  6.         <ul>
  7.             <li>{name}</li>
  8.             <li>{age}</li>
  9.             <li>{sex}</li>
  10.         </ul>
  11.     )
  12.   }
  13. }
  14. export { Person }
复制代码
调用组件(通过props传值)
  1. import { Person } from './components/propsReact'
  2. function App() {
  3.   //return <h2>我是函数式组件<MyClassCom></MyClassCom></h2>
  4.   return (
  5.     <div>
  6.       <Person name="张三" age="18" sex="男"></Person>
  7.       <Person name="李四" age="19" sex="女"></Person>
  8.     </div>
  9.   )
  10. }
  11. export default App
复制代码
其实这里就是一个父传子的操作,跟vue思想差不多
3.2.2、props限制

范例限制:
  1. import { Component } from "react";
  2. import PropTypes from "prop-types";//需要安装库
  3. class Person extends Component<{ name: string,age:string,sex:string }> {
  4.   render() {
  5.     const {name, age , sex} = this.props;
  6.     return (
  7.         <ul>
  8.             <li>{name}</li>
  9.             <li>{age}</li>
  10.             <li>{sex}</li>
  11.         </ul>
  12.     )
  13.   }
  14. }
  15. Person.propTypes = {
  16.   name: PropTypes.string.isRequired,//isRequired是必填项
  17.   age: PropTypes.string.isRequired,
  18.   sex: PropTypes.string.isRequired,
  19. };
  20. export { Person }
复制代码
  1. import { Person } from './components/propsReact'
  2. function App() {
  3.   //return <h2>我是函数式组件<MyClassCom></MyClassCom></h2>
  4.   return (
  5.     <div>
  6.       <Person name="asd" age="18" sex="男"></Person>
  7.       <Person name="李四" age="19" sex="女"></Person>
  8.     </div>
  9.   )
  10. }
  11. export default App
复制代码
简写方式:
  1. import { Component } from "react";
  2. import PropTypes from "prop-types";
  3. class Person extends Component<{ name: string; age: string; sex: string }> {
  4.   static propTypes = {
  5.     name: PropTypes.string.isRequired,
  6.     age: PropTypes.string.isRequired,
  7.     sex: PropTypes.string.isRequired,
  8.   };
  9.   static defaultProps = {
  10.     name: "张三",
  11.     age: "18",
  12.     sex: "男",
  13.   };
  14.   render() {
  15.     const { name, age, sex } = this.props;
  16.     return (
  17.       <ul>
  18.         <li>{name}</li>
  19.         <li>{age}</li>
  20.         <li>{sex}</li>
  21.       </ul>
  22.     );
  23.   }
  24. }
  25. export { Person };
复制代码
3.2.3、函数组件使用props

函数式组件只能使用props,其他两个属性没法用
  1. import { Component } from "react";
  2. import PropTypes from "prop-types";
  3. class Person extends Component<{ name: string; age: string; sex: string }> {
  4.   static propTypes = {
  5.     name: PropTypes.string.isRequired,
  6.     age: PropTypes.string.isRequired,
  7.     sex: PropTypes.string.isRequired,
  8.   };
  9.   static defaultProps = {
  10.     name: "张三",
  11.     age: "18",
  12.     sex: "男",
  13.   };
  14.   render() {
  15.     const { name, age, sex } = this.props;
  16.     return (
  17.       <ul>
  18.         <li>{name}</li>
  19.         <li>{age}</li>
  20.         <li>{sex}</li>
  21.       </ul>
  22.     );
  23.   }
  24. }
  25. function Person1(props: { name: string; age: string; sex: string }) {
  26.     const { name, age, sex } = props;
  27.     return (
  28.       <ul>
  29.         <li>{name}</li>
  30.         <li>{age}</li>
  31.         <li>{sex}</li>
  32.       </ul>
  33.     );
  34. }
  35. Person1.prototype = {
  36.     name: PropTypes.string.isRequired,
  37.     age: PropTypes.string.isRequired,
  38.     sex: PropTypes.string.isRequired,
  39. }
  40. export { Person, Person1};
复制代码

  1. import { Person,Person1 } from './components/propsReact'
  2. function App() {
  3.   //return <h2>我是函数式组件<MyClassCom></MyClassCom></h2>
  4.   return (
  5.     <div>
  6.       <Person name="张三" age="18" sex="男"></Person>
  7.       <Person name="李四" age="19" sex="女"></Person>
  8.       <Person></Person>
  9.       <Person1 name="张三" age="108" sex="男"></Person1>
  10.     </div>
  11.   )
  12. }
  13. export default App
复制代码
总结:
1、每个组件都有props属性
2、组件所有的标签属性都会存在props中
3、组件内部不要修改props
4、通过标签属性从组件外部转到达内部的变化的数据
3.3、refs

3.3.1、字符串范例写法:

存在效率问题(不推荐使用)
  1. import React from "react";
  2. class RefsDemo extends React.Component{
  3.     showData  = () => {
  4.         console.log(this)
  5.         const {input1} = this.refs
  6.         alert(input1.value)
  7.     }
  8.     showData2 = () => {
  9.         const {input2} = this.refs
  10.         alert(input2.value)
  11.     }
  12.     render(): React.ReactNode {
  13.         return (
  14.             <div>
  15.                 <input ref="input1" type="text" />
  16.                 <button onClick={this.showData}></button>
  17.                 <input ref="input2" onBlur={this.showData2} type="text" />
  18.             </div>
  19.         )
  20.     }
  21. }
  22. export default RefsDemo
复制代码
3.3.2、回调函数情势

  1. import React from "react";
  2. class RefsDemo extends React.Component{
  3.     showData  = () => {
  4.         console.log(this)
  5.         const {input1} = this
  6.         alert(input1.value)
  7.     }
  8.     showData2 = () => {
  9.         const {input2} = this
  10.         alert(input2.value)
  11.     }
  12.     render(): React.ReactNode {
  13.         return (
  14.             <div>
  15.                 <input ref={c=>this.input1=c} type="text" />
  16.                 <button onClick={this.showData}></button>
  17.                 <input ref={c=>this.input2=c} onBlur={this.showData2} type="text" />
  18.             </div>
  19.         )
  20.     }
  21. }
  22. export default RefsDemo
复制代码
注意:
        1、这样写会有 副作用
        2、可以把方法抽出来放在render里面作为方法调用
3.3.3、React.createRef()钩子的使用

  1. import React from "react";
  2. class RefsDemo extends React.Component{
  3.     /**每一个createRef都是单独的,用来获取组件中的元素 */
  4.     myRef = React.createRef()
  5.     myRef1 = React.createRef()
  6.     showData = () => {
  7.         console.log(this.myRef.current.value)
  8.     }
  9.     showData2 = () => {
  10.         console.log(this.myRef1.current.value)
  11.     }
  12.     render(): React.ReactNode {
  13.         return (
  14.             <div>
  15.                 <input ref={this.myRef} type="text" />
  16.                 <button onClick={this.showData}></button>
  17.                 <input ref = {this.myRef1} onBlur={this.showData2}
  18.                  type="text" />
  19.             </div>
  20.         )
  21.     }
  22. }
  23. export default RefsDemo
复制代码
总结ref:
        1、尽可能避免字符串方法的使用
        2、内联用的最多,第三个比较繁琐,要使用钩子
4、事件处理

4.1、非受控组件

  1. import React from "react";
  2. class Login extends React.Component {
  3.     handleSubmit = (e) => {
  4.         e.preventDefault()//阻止默认行为
  5.         const { username, password } = this
  6.         console.log(username, password)
  7.         alert(`用户名:${username.value} 密码:${password.value}`)
  8.     }
  9.     render(): React.ReactNode {
  10.         return (
  11.             <div>
  12.                 <form action="https://www.baidu.com" onSubmit={this.handleSubmit}>
  13.                     用户名:<input ref={c=>this.username = c} type="text" name="username" />
  14.                     密码:<input ref = {c=>this.password = c} type="password" name="password" />
  15.                     <button type="submit">登录</button>
  16.                 </form>
  17.             </div>
  18.         )
  19.     }
  20. }
  21. export default Login;
复制代码
4.2、受控组件

  1. import React from "react";
  2. class Login extends React.Component {
  3.     state: Readonly<{}> = {
  4.         username: "",
  5.         password: ""
  6.     }
  7.     saveUsername = (e) =>{
  8.         this.setState({
  9.             username: e.target.value
  10.         })
  11.     }
  12.     savePassword = (e) =>{
  13.         this.setState({
  14.             password: e.target.value
  15.         })
  16.     }
  17.     handleSubmit = (e) => {
  18.         e.preventDefault()//阻止默认行为
  19.         const { username, password } = this.state
  20.         console.log(username, password)
  21.         alert(`用户名:${username} 密码:${password}`)
  22.     }
  23.     render(): React.ReactNode {
  24.         return (
  25.             <div>
  26.                 <form action="https://www.baidu.com" onSubmit={this.handleSubmit}>
  27.                     用户名:<input onChange={this.saveUsername} type="text" name="username" />
  28.                     密码:<input onChange={this.savePassword} type="password" name="password" />
  29.                     <button type="submit">登录</button>
  30.                 </form>
  31.             </div>
  32.         )
  33.     }
  34. }
  35. export default Login;
复制代码
注意:
1、受控组件能够避免ref的使用
2、现用现取黑白受控,维护状态的是受控组件
5、高阶函数+函数柯里化

高级函数:
        1、若A函数,按接的参数是一个函数,那么A就是高阶函数
        2、若A函数,调用的返回值依然是一个函数,那么A就可以称为高阶函数
  常见的高阶函数:Promise、setTimeout、arr.map()等
函数的柯里化:通过函数调用继续返回函数的方式,实现多次吸收参数末了统一处理的函数
eg:
  1. import React from "react";
  2. class Login extends React.Component {
  3.      saveFromData = (typename) =>{
  4.         return (event) => {
  5.             this.setState({
  6.                 [typename]: event.target.value
  7.             })
  8.         }
  9.     }
  10.     render(): React.ReactNode {
  11.         return (
  12.             <div>
  13.                     用户名:<input onChange={this.saveFromData('username')} type="text" name="username" />
  14.                     密码:<input onChange={this.saveFromData('password')} type="password" name="password" />
  15.                     <button type="submit">登录</button>
  16.             </div>
  17.         )
  18.     }
  19. }
  20. export default Login;
复制代码
6、生命周期

组件挂载完毕和将要卸载的调用:
  1. import React from "react";
  2. class Login extends React.Component {
  3.     // 组件挂载的时候调用
  4.     componentDidMount(): void {
  5.       this.timer =   setTimeout(() => {
  6.            console.log(11111)
  7.         }, 1000)
  8.     }
  9.     // 挂载的组件卸载前 的调用
  10.     componentWillUnmount(): void {
  11.         clearTimeout(this.timer)
  12.     }
  13.     render(): React.ReactNode {
  14.         return (
  15.             <div>
  16.             </div>
  17.         )
  18.     }
  19. }
  20. export default Login;
复制代码
 6.1、组件挂载流程

6.1.1、生命周期(旧)


eg:
  1. import { Component } from "react";
  2. class Count extends Component {
  3.   constructor(props) {
  4.     super(props);
  5.     this.state = {
  6.       count: 0,
  7.     };
  8.     this.name = "count";
  9.     console.log("count-constructor");
  10.   }
  11.   add = () => {
  12.     this.setState({
  13.       count: this.state.count + 1,
  14.     });
  15.   };
  16.   foce = () => {
  17.     this.forceUpdate();
  18.   };
  19.   //   组件将要挂载的钩子
  20.   componentWillMount() {
  21.     console.log("componentWillMount");
  22.   }
  23.   //   组件挂载完成的钩子
  24.   componentDidMount() {
  25.     console.log("componentDidMount");
  26.   }
  27.   // 组件将要卸载
  28.   componentWillUnmount() {
  29.     console.log("componentWillUnmount");
  30.   }
  31.   // 组件是否需要更新--阀门
  32.   showldComponentUpdate() {
  33.     console.log("showldComponentUpdate");
  34.     return true;
  35.   }
  36.   // 组件将要更新
  37.   componentWillUpdate() {
  38.     console.log("componentWillUpdate");
  39.   }
  40.   // 组件更新完成
  41.   componentDidUpdate() {
  42.     console.log("componentDidUpdate");
  43.   }
  44.   render() {
  45.     return (
  46.       <div>
  47.         <h2>当前求和为:{this.state.count}</h2>
  48.         <button onClick={this.add}>点我+1</button>
  49.         <button onClick={this.foce}>强制更新组件</button>
  50.         <A name={this.name} content={this.state.count} />
  51.       </div>
  52.     );
  53.   }
  54. }
  55. class A extends Component {
  56.     //这个钩子比较奇特,只有操作更新的时候才会调用,第一次传的时候不调用,此处就是操作+1的时候才调用--将要废弃
  57.   componentWillReceiveProps(props) {
  58.       console.log("componentWillReceiveProps",props);
  59.   }
  60.   render() {
  61.     return (
  62.       <div>
  63.         我是子组件{this.props.name}
  64.         <p>{this.props.content}</p>
  65.       </div>
  66.     );
  67.   }
  68. }
  69. export default Count;
复制代码
总结:(标红的是常用的)
        1.初始化阶段:由ReactDoM.render()触发---初次渲染
                A、constructor()
                B、componentWillMount() //将要废弃
                C、render()
                D、componentDidMount() ---常用于做初始化数据(一般用于网络请求、订阅消息、开启定时器)
        2.更新阶段:由组件内部this.setsate()或父组件render触发
                A、shouldComponentUpdate()
                B、componentWillUpdate()  //将要废弃
                C、render()
                D、componentDidUpdate()
        3.卸线组件:由ReactD0M.unmountComponentAtNode()触发
                A、componentWillUnmount() --常用于收尾(关闭定时器、取消订阅等)

6.1.2、生命周期(新>=16.4)

 官网的周期图:

 eg:
  1. import { Component, createRef } from "react";
  2. class Count extends Component {
  3.   constructor(props) {
  4.     super(props);
  5.     this.state = {
  6.       count: 0,
  7.     };
  8.     this.name = "count";
  9.     console.log("count-constructor");
  10.   }
  11.   add = () => {
  12.     this.setState({
  13.       count: this.state.count + 1,
  14.     });
  15.   };
  16.   foce = () => {
  17.     this.forceUpdate();
  18.   };
  19.   //若state的值在任何时候取决于props的值,则使用getDerivedStateFromProps ---使用场景及其罕见
  20.   // static getDerivedStateFromProps(props,state) {
  21.   //   console.log("getDeruvedStateFromProps");
  22.   //   // return console.log(props,state);
  23.   // }
  24.   
  25.   //   组件挂载完成的钩子
  26.   componentDidMount() {
  27.     console.log("componentDidMount");
  28.   }
  29.   // 组件将要卸载
  30.   componentWillUnmount() {
  31.     console.log("componentWillUnmount");
  32.   }
  33.   // 组件是否需要更新--阀门
  34.   showldComponentUpdate() {
  35.     console.log("showldComponentUpdate");
  36.     return true;
  37.   }
  38.   // 组件更新前获取快照
  39.   getSnapshotBeforeUpdate() {
  40.     console.log("getSnapshotBeforeUpdate");
  41.     return null
  42.   }
  43.   // 组件更新完成
  44.   componentDidUpdate(preProps, preState,Shouwkong) {
  45.     console.log("componentDidUpdate",preProps,preState,Shouwkong);
  46.   }
  47.   render() {
  48.     return (
  49.       <div>
  50.         <h2>当前求和为:{this.state.count}</h2>
  51.         <button onClick={this.add}>点我+1</button>
  52.         <DomList />
  53.       </div>
  54.     );
  55.   }
  56. }
  57. export default Count;
  58. /**
  59. * 列表滚动渲染案例
  60. */
  61. class DomList extends Component {
  62.   constructor(props) {
  63.     super(props);
  64.     this.listRef = createRef();
  65.     this.state = {
  66.       newsArr: [],
  67.     };
  68.   }
  69.   componentDidMount() {
  70.     setInterval(() => {
  71.       const { newsArr } = this.state;
  72.       const news = '商品' + (newsArr.length + 1);
  73.       this.setState({
  74.         newsArr: [news, ...newsArr],
  75.       });
  76.     }, 1000);
  77.   }
  78.   getSnapshotBeforeUpdate(prevProps, prevState) {
  79.     return this.listRef.current ? this.listRef.current.scrollHeight : null;
  80.   }
  81.   componentDidUpdate(prevProps, prevState, snapshot) {
  82.     if (this.listRef.current) {
  83.       this.listRef.current.scrollTop += this.listRef.current.scrollHeight - snapshot;
  84.     }
  85.   }
  86.   render() {
  87.     return (
  88.       <div className="list" ref={this.listRef} style={{ height: '300px', overflow: 'auto' }}>
  89.         {this.state.newsArr.map((item, index) => (
  90.           <p key={index} className="news">{item}</p>
  91.         ))}
  92.       </div>
  93.     );
  94.   }
  95. }
复制代码
总结:
(标红的是常用的)
        1.初始化阶段:由ReactDoM.render()触发---初次渲染
                A、constructor()
                B、getDerivedStateFromProps
                C、render()
                D、componentDidMount() ---常用于做初始化数据(一般用于网络请求、订阅消息、开启定时器)
        2.更新阶段:由组件内部this.setsate()或父组件render触发
                A、getDerivedStateFromProps
                B、showldComponentUpdate
                C、render()
                D、getSnapshotBeforeUpdate
                 E、componentDidUpdate
        3.卸线组件:由ReactD0M.unmountComponentAtNode()触发
                A、componentWillUnmount() --常用于收尾(关闭定时器、取消订阅等)
7、diffing算法


 

   
  8、脚手架配置

  8.1、署理配置

方法1:
        在package.json追加如下配置:
  1. "proxy":"http://localhost:5000"
复制代码
说明:
        1、优点:配置简朴,前端请求资源时可以不加任何前缀
        2、缺点:不能配置多个署理
        3、工作方式:当请求3000不存在的时间,资源请求转发给5000
方法2:
1、第一步:创建署理配置文件
        在src下创建配置配置文件:src/setupProxy.js
2、编写setupProxy.js配置具体署理规则:
  1. const proxy = require('http-proxy-middleware');
  2. module.exports = function (app) {
  3.   app.use(proxy('/api', { //api是需要转发的请求(所有带有/api标识的请求都会转发给后台-5000)
  4.     target: 'http://localhost:3000' , //配置转发目标地址(能返回苏剧的服务器地址)
  5.     changeOrigin: true,//控制服务器接收请求头中Host字段的值,
  6.     /**
  7.      * 重写请求路径
  8.      * 例如:
  9.      *  请求地址:http://localhost:3000/api/user/list
  10.      *  重写之后:http://localhost:5000/user/list
  11.      */
  12.     pathRewrite: {
  13.       '^/api': ''//去除请求地址中的/api,保证能正常请求到接口
  14.     },  
  15.     }
  16. ));
  17. };
复制代码
说明:
        1、优点:可以配置多个署理,可以机动的控制请求是否走署理
        2、配置繁琐,前端请求资源时必须加前缀
9、消息订阅-发布机制

1、工具库:PubSubJS
2、npm install pubsub-js
3、使用: 
                3.1、improt PubSub from 'pubsub-js'
                3.2、PubSub.subscribe("del"mfunction(data){})//订阅
                3.3、PubSub.publish(‘del’,data)//发布消息
eg:
  1. 父组件:
  2. import React, { Component } from 'react'
  3. import A from "../components/A"
  4. import B from "../components/B"
  5. export default class test extends Component {
  6.   render() {
  7.     return (
  8.       <div>
  9.               <A/>
  10.               <B/>
  11.       </div>
  12.     )
  13.   }
  14. }
  15. A子组件--发布
  16. import React, { Component } from 'react'
  17. import pubsub from 'pubsub-js'
  18. export default class A extends Component {
  19.     componentDidMount(){
  20.         pubsub.publish('test', 'test')
  21.     }
  22.   render() {
  23.     return (
  24.       <div>A</div>
  25.     )
  26.   }
  27. }
  28. B子组件--订阅
  29. import React, { Component } from 'react'
  30. import pubsub from 'pubsub-js'
  31. export default class B extends Component {
  32.     componentDidMount() {
  33.         pubsub.subscribe('test',(msg,data)=>{
  34.             console.log(msg,data)
  35.         })
  36.     }
  37.     componentWillUnmount() {
  38.         pubsub.unsubscribe('test')
  39.     }
  40.   render() {
  41.     return (
  42.       <div>B</div>
  43.     )
  44.   }
  45. }
复制代码
10、路由(参考另外一个18+的教程)

参考链接:Home v6.24.0 | React Router
对比:


 基本使用的三种方式:(16)


 

 11、编程式导航

方法调用:


通过onclick调用:

detail组件吸收:

 12、withRouter的使用





 13、BrowserRouter与HashRouter区别




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

祗疼妳一个

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表