1.高阶组件对props的劫持

  1. about.jsx
  2. import React, { PureComponent } from 'react'
  3. import Total from "../Total"
  4. class About extends PureComponent {
  5. render() {
  6. return (
  7. <h1>About:{`昵称:${this.props.nickname}等级:${this.props.level}区域:${this.props.region}`}</h1>
  8. )
  9. }
  10. }
  11. export default Total(About)
  1. home.jsx
  2. import React, {PureComponent } from 'react'
  3. import Total from "../Total"
  4. class Home extends PureComponent {
  5. render() {
  6. return (
  7. <h1>Home:{`昵称:${this.props.nickname}等级:${this.props.level}区域:${this.props.region}`}</h1>
  8. )
  9. }
  10. }
  11. export default Total(Home)
  1. const total=(Newcomponent)=>{
  2. return props=>{
  3. return <Newcomponent {...props} region="中国"/>
  4. }
  5. }
  6. export default total

2.鉴权操作

相当于一层拦截器,例如判断当前组件是否是登录状态

  1. //AredyLogin.jsx
  2. import React, { Component } from 'react'
  3. import NewLogin from "./NewLogin"
  4. class AredyLogin extends Component {
  5. render() {
  6. return (
  7. <div>
  8. 这是个已经登录状态的组件
  9. </div>
  10. )
  11. }
  12. }
  13. export default NewLogin(AredyLogin)
  1. //外层函数组件判断是否处于登录状态
  2. import StartLogin from "./StartLogin"
  3. const isLogin=(Login)=>{
  4. return props=>{
  5. if(props.login){
  6. return <Login {...props}/>
  7. }
  8. else{
  9. return <StartLogin/>
  10. }
  11. }
  12. }
  13. export default isLogin
  1. //没有登录状态的组件
  2. //StartLogin.jsx
  3. import React, { Component } from 'react'
  4. export default class StartLogin extends Component {
  5. render() {
  6. return (
  7. <div>
  8. 没有登录吧,就跳转到这个组件
  9. </div>
  10. )
  11. }
  12. }
  1. app.js
  2. function App() {
  3. return (
  4. <div className="App">
  5. 我是app组件
  6. <AredyLogin login={true}></AredyLogin>
  7. </div>
  8. );
  9. }
  10. export default App;