多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
![](https://img.kancloud.cn/60/75/6075b3f8fb999b7ae1bf85555e62a00b_773x378.png) ![](https://img.kancloud.cn/3d/d3/3dd307edf706bc7825bb6e7cf57cec03_972x620.png) ![](https://img.kancloud.cn/4b/c3/4bc38bb35338355011a5bf21c8c60930_697x524.png) ~~~ import React from 'react' // 高阶组件 const withMouse = (Component) => { class withMouseComponent extends React.Component { constructor(props) { super(props) this.state = { x: 0, y: 0 } } handleMouseMove = (event) => { this.setState({ x: event.clientX, y: event.clientY }) } render() { return ( <div style={{ height: '500px' }} onMouseMove={this.handleMouseMove}> {/* 1. 透传所有 props 2. 增加 mouse 属性 */} <Component {...this.props} mouse={this.state}/> </div> ) } } return withMouseComponent } const App = (props) => { const a = props.a const { x, y } = props.mouse // 接收 mouse 属性 return ( <div style={{ height: '500px' }}> <h1>The mouse position is ({x}, {y})</h1> <p>{a}</p> </div> ) } export default withMouse(App) // 返回高阶函数 ~~~