1.概述:
2.思路分析:
3.使用步骤
- import React from 'react';
- import ReactDOM from 'react-dom';
- import MyEditor from './components/MyEditor';
-
- //React脚手架引入图片的方式
- // import img from './images/open.png'
-
- // 创建高阶组件
- function withMouse (WrappedComponent){
- //该组件提供复用的状态代码逻辑
- class Mouse extends React.Component{
- state={
- x:0,
- y:0
- }
-
-
- handleMouseMouve= e=>{
- this.setState({
- x:e.clientX,
- y:e.clientY,
- })
- }
-
- //监听鼠标移动事件
- componentDidMount(){
- window.addEventListener('mousemove',this.handleMouseMouve)
- console.log('listener')
- console.log(this.state.x)
- console.log(this.state.y)
- }
-
- //解除绑定
- componentWillUnmount(){
- window.removeEventListener('mousemove',this.handleMouseMouve)
- }
-
- render(){
- return (
- <WrappedComponent {...this.state}/>
- )
-
- }
-
- }
- return Mouse
- }
-
- //用来测试高阶组件
- const Position = props => {
- <p>
- X:{props.x} y:{props.y}
- p>
- }
-
- // const Open = props =>{
- //
![]()
- // src={img}
- // alt='open'
- // style={{
- // position:'absolute',
- // top:props.y,
- // left:props.x
- // }}
- // />
- // }
-
-
- //获取增强后的组件:
- const MousePosition = withMouse(Position)
-
- // const OpenPosition = withMouse(Open)
-
- class App extends React.Component{
- render(){
- return(
- <div>
- <h1>高阶组件h1>
- <MousePosition/>
- {/* <OpenPosition>OpenPosition> */}
- div>
- )
- }
- }
-
- ReactDOM.render(<App/>,
-
- document.getElementById('root'))
不知道为什么在浏览器无法显示效果,但也没有报错!!