• 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. ```
  • 相关阅读:
    旭日x3派与STM32串口通信
    【map的实际应用,学习map,不用=白学】3302. 表达式求值【如何得到运算符优先级?自己过一遍示例即可明白】【怎么比较运算符的优先级?map】
    Docker镜像制作及使用Dockerfile制作镜像
    SY6288AAAC低耗配电开关USB限流开关600MA
    SpringBoot-WebSocket浏览器-服务器双向通信
    使用cpolar为Ubuntu的网站配置有特色的地址
    【Rust指南】详解注释|函数|条件语句|循环语句
    【C++】详解map和set基本接口及使用
    HCM 初学 ( 一 ) - 简介
    C#中的双缓冲(转)
  • 原文地址:https://blog.csdn.net/weixin_51642358/article/details/126454394