简体   繁体   English

如何将这个Component Class变成一个纯函数的无状态组件(Typescript + React)

[英]How to turn this Component Class into a pure function Stateless Component (Typescript + React)

Hello I had this component and a container component class and it had some state. 您好,我有这个组件和一个容器组件类,它有一些状态。 I realized that I do not need it to manage state anymore and I want to turn it into a sateless component so from "export class LeftNavigation extends React.Component {" to "export const LeftNavigation: React.StatelessComponent =props => { 我意识到我不再需要它来管理状态,而是想将其变成一个无状态的组件,因此从“导出类LeftNavigation扩展了React.Component {”到“导出const LeftNavigation:React.StatelessComponent = props => {

import * as React from "react";
import { NavLink, Link } from 'react-router-dom';

export interface LeftNavigationProps {
    title: string;
    path: string;
}

export class LeftNavigationItem {
    title: string;
    path: string;

    constructor(title: string, path: string) {
        this.title = title;
        this.path = path;
    }
}

// 'LeftNavigationProps' describes the shape of props.
export class LeftNavigation extends React.Component<LeftNavigationProps, {}> {
    static items: Array<LeftNavigationItem> = [
        new LeftNavigationItem('On demand tests', '/ondemandtests'),
        new LeftNavigationItem('Fleet status',    '/fleetstatus'  )
    ];

    constructor(props: LeftNavigationProps) {
        super(props);
        this.state = { focused: 0 };
    }

    componentWillMount() {

    }

    render() {
        var self = this;

        return (
            <div className="sidebar-container">
                <div className="page-sidebar">
                    <div className="nav-menu-stack">
                        <Link className="active" to={this.props.path}>{this.props.title}</Link>
                        <div className='subnav-menu'> {
                            LeftNavigation.items.map(function(m, index) {
                                return <NavLink key={m.path} activeClassName="active" to={self.props.path + m.path}>{m.title}</NavLink>;
                            })
                        }
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

Give this a shot: 试一下:

import * as React from "react";
import { NavLink, Link } from 'react-router-dom';

export interface LeftNavigationProps {
    title: string;
    path: string;
}

export class LeftNavigationItem {
    title: string;
    path: string;

    constructor(title: string, path: string) {
        this.title = title;
        this.path = path;
    }
}

const LeftNavigation = (props: LeftNavigationProps) => {
    const items: Array<LeftNavigationItem> = [
        new LeftNavigationItem('On demand tests', '/ondemandtests'),
        new LeftNavigationItem('Fleet status', '/fleetstatus')
    ];
    return (
        <div className="sidebar-container">
            <div className="page-sidebar">
                <div className="nav-menu-stack">
                    <Link className="active" to={props.path}>{props.title}</Link>
                    <div className='subnav-menu'> {
                        items.map(function (m, index) {
                            return <NavLink key={m.path} activeClassName="active"
                                            to={props.path + m.path}>{m.title}</NavLink>;
                        })
                    }
                    </div>
                </div>
            </div>
        </div>
    );
};

export default LeftNavigation;

Notice that it's just a regular old function and that the props are passed in directly. 请注意,这只是一个常规的旧函数,而道具则直接传递进来。 No this keyword. 没有this关键字。 Also, items is declared inside the function itself before the return statement. 此外, items是return语句之前函数本身内部声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM