1.高阶组件的作用:实现组件的状态逻辑复用
2.高阶组件的使用步骤:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// 高阶组件的基本使用
// 1. 创建一个函数,以with开头
// 2. 指定函数的参数,以大写字母开头
// 3. 在函数内部创建一个类组件,同时将状态通过props传递给参数组件
function withMouse(WrappedComponent) {
class Mouse extends React.Component {
state = {
x: '',
y: ''
}
handleMove = (e) => {
this.setState({
x: e.pageX,
y: e.pageY
})
}
componentDidMount() {
window.addEventListener('mousemove', this.handleMove)
}
componentWillUnmount() {
window.removeEventListener('mousemove', this.handleMove)
}
render() {
return <WrappedComponent {...this.state} />
}
}
return Mouse
}
// 创建一个包含渲染ui的组件
function Position(props) {
return (
<div>
<p>x: {props.x}</p>
<p>y: {props.y}</p>
</div>
)
}
// 调用封装状态和逻辑的方法包装Position
const MousePosition = withMouse(Position)
class App extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<div>
<h3>高阶组件</h3>
<MousePosition></MousePosition>
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'))
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// 高阶组件,实现组件的复用
// 创建高阶组件的步骤
// 1. 创建一个以with开头的函数, 参数值必须以大写字母开头
function withMouse(WrapedComponent) {
// 2. 封装一个带有状态和操作方法的组件
class Mouse extends React.Component {
state = {
x: 0,
y: 0
}
handleMove = (e) => {
this.setState({
x: e.pageX,
y: e.pageY
})
}
componentDidMount() {
window.addEventListener('mousemove', this.handleMove)
}
render() {
return <WrapedComponent {...this.state} />
}
}
// 如果组件被复用多次 ,会存在一个问题, 多个组件名称一致,不方便进行调试,因此需要设置displayName,以区分组件的不同
Mouse.displayName = `withMouse${getDisplayName(WrapedComponent)}`
function getDisplayName(WrappedComponent) {
return WrapedComponent.displayName || WrappedComponent.name || 'Component'
}
return Mouse
}
// 定义一个ui组件
function Position(props) {
return (
<div>x: {props.x}, y: {props.y}</div>
)
}
function Kill(props) {
return (
<div>
<h2>x: {props.x}, y: {props.y}</h2>
</div>
)
}
const MousePosition = withMouse(Position)
const TwoPosition = withMouse(Kill)
class App extends React.Component {
render() {
return (
<div>
<MousePosition />
<TwoPosition />
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'))
</script>
</body>
</html>
如果需要传值,需要在封装的高阶组件返回值中添加{…this.props},否则,在是接收不到传递过来的值的,可以在ui组件中打印查看结果
完整代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// 高阶组件,实现组件的复用
// 创建高阶组件的步骤
// 1. 创建一个以with开头的函数, 参数值必须以大写字母开头
function withMouse(WrapedComponent) {
// 2. 封装一个带有状态和操作方法的组件
class Mouse extends React.Component {
state = {
x: 0,
y: 0
}
handleMove = (e) => {
this.setState({
x: e.pageX,
y: e.pageY
})
}
componentDidMount() {
window.addEventListener('mousemove', this.handleMove)
}
render() {
return <WrapedComponent {...this.state} {...this.props}/>
}
}
// 如果组件被复用多次 ,会存在一个问题, 多个组件名称一致,不方便进行调试,因此需要设置displayName,以区分组件的不同
Mouse.displayName = `withMouse${getDisplayName(WrapedComponent)}`
function getDisplayName(WrappedComponent) {
return WrapedComponent.displayName || WrappedComponent.name || 'Component'
}
return Mouse
}
// 定义一个ui组件
function Position(props) {
console.log(props)
return (
<div>x: {props.x}, y: {props.y}</div>
)
}
function Kill(props) {
return (
<div>
<h2>x: {props.x}, y: {props.y}</h2>
</div>
)
}
const MousePosition = withMouse(Position)
const TwoPosition = withMouse(Kill)
class App extends React.Component {
render() {
return (
<div>
<MousePosition value={19}/>
<TwoPosition />
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'))
</script>
</body>
</html>