首页 > 解决方案 > 转换为类组件?

问题描述

当我将其转换为类组件时,道具正在被定义。所以我们应该如何编写这是类组件

const Admin = props => {
      console.log(props);
      const username  = props.location.username;
        
      return (
        <div>
          <NavLink to="/" activeClassName="active">
            LOGOUT
          </NavLink>
          <br/>
          <br/>
         <Button>hello</Button>
            <h2> Username </h2> {username}
            <h1>child component-MILAN</h1>
          </div>
      );

标签: javascriptreactjs

解决方案


类组件

import React, { Component } from 'react';

export default class Admin extends Component {
  render() {
    const username = this.props.location.username;
    return (
      <div>
        <NavLink to="/" activeClassName="active">
          LOGOUT
        </NavLink>
        <br />
        <br />
        <Button>hello</Button>
        <h2> Username </h2> {this.username}
        <h1>child component-MILAN</h1>
      </div>
    );
  }
}

推荐阅读