• React组件复用


    React组件复用

    • render props 模式

      • 思路:将要复用的 state 和操作 state 的方法封装到一个组件中

      • 使用步骤:

        • 创建复用组件

        • 将要复用的数据、方法作为 props.render(state) 方法的参数,暴露到组件外

        • 使用 props.render() 的返回值作为要渲染的内容

    1. ```javascript
    2. import propTypes from 'prop-types'
    3. // 要复用的组件
    4. class Mouse extends React.Component {
    5. state = {
    6. x : 0,
    7. y : 0
    8. }
    9. handleMove = e => {
    10. this.setState({
    11. x : e.clientX,
    12. y : e.clientY
    13. })
    14. }
    15. // 监听鼠标移动事件
    16. componentDidMount() {
    17. window.addEventListener('mousemove',this.handleMove)
    18. }
    19. // 卸载鼠标移动事件
    20. componentWillUnmount() {
    21. window.removeEventListener('mousemove', this.handleMove)
    22. }
    23. // 将数据作为参数返回
    24. render() {
    25. return this.props.render(this.state)
    26. }
    27. }
    28. //添加Props校验
    29. Mouse.propTypes = {
    30. children : propTypes.func.isRequired
    31. }
    32. class App extends React.Component{
    33. render() {
    34. return (
    35. // 获取到参数 用返回值进行使用
    36. <Mouse render={(mouse) => {
    37. return

    38. 鼠标当前位置:{mouse.x},{mouse.y}
    39. }} />
    40. )
    41. }
    42. }
    43. ```
    • 高阶组件(HOC)

      • 简单来说:高阶组件就是提供一些数据方法,然后将普通组件套到高阶组件中,普通组件就有高阶组件中定义的那些方法和数据了

      • 使用步骤:

        • 创建一个函数,名称以 with 开头

        • 指定函数参数,参数以大写字母开头(表示要渲染的组件)

        • 在函数内部创建一个类组件,提供要复用的逻辑代码,并返回

        • 在该组件中,渲染参数组件,同时将状态传递通过props 传递给参数组件

        • 调用该高阶组件,传入需要增强的组件,通过返回值拿到增强后的组件,并将其渲染到页面上

        • 需要设置 displayName,方便后期调试(不设置的话,使用这个高阶组件的所有组件显示在调试工具上的组件名都是一样的)

    1. ```javascript
    2. // 高阶组件
    3. function withMouse(WrappedComponent) {
    4. // 提供要复用的状态逻辑
    5. class Mouse extends React.Component{
    6. state = {
    7. x : 0,
    8. y : 0
    9. }
    10. handleMouseMove = e => {
    11. this.setState({
    12. x : e.clientX,
    13. y : e.clientY
    14. })
    15. }
    16. // 鼠标状态逻辑
    17. componentDidMount() {
    18. window.addEventListener('mousemove', this.handleMouseMove)
    19. }
    20. // 解绑鼠标状态逻辑
    21. componentWillUnmount() {
    22. window.removeEventListener('mousemove', this.handleMouseMove)
    23. }
    24. render() {
    25. //将组件 return 出去 带上数据
    26. return <WrappedComponent {...this.state}{...this.props} />
    27. }
    28. }
    29. // 设置 displayName作为组件名称
    30. Mouse.displayName = `WithMouse${getDisplayName(WrappedComponent)}`
    31. return Mouse
    32. }
    33. // 获取组件名称
    34. function getDisplayName(WrappedComponent) {
    35. return WrappedComponent.displayName || WrappedComponent.name || 'Component';
    36. }
    37. // 需要增强的组件
    38. const Position = props => (
    39. <p>
    40. 鼠标当前位置:x : {props.x}, y : {props.y}
    41. </p>
    42. )
    43. // 获取到增强后的组件
    44. const MousePosition = withMouse(Position)
    45. class App extends React.Component{
    46. render() {
    47. return (
    48. <div>
    49. <MousePosition />
    50. </div>
    51. )
    52. }
    53. }
    54. const container = document.getElementById('root')
    55. const root = createRoot(container)
    56. root.render(<App />)
    57. ```
  • 相关阅读:
    采集网页数据保存到文本文件---爬取古诗文网站
    【遮天】韩老魔被灭小囡囡现身,好消息叶凡终于不跑酷了,但有坏消息
    Mathorcup数学建模竞赛第四届-【妈妈杯】C题:家庭暑假旅游套餐的设计(附MATLAB代码)
    Qt第六十四章:QSplitter(分离部件)的使用
    图论32(Leetcode210课程表2)
    【TensorFlow2 之013】TensorFlow-Lite
    敏捷思维&敏捷管理工具
    鸿蒙应用开发-初见:入门知识、应用模型
    【LeetCode】二叉树题总结(持续更新)
    从客户端到服务器
  • 原文地址:https://blog.csdn.net/weixin_51642358/article/details/126454394