React Component access Props

问题描述 投票:0回答:1

是否可以访问React组件的内部属性?

React Component的示例通常将道具类型显示为HTML / XML内部属性:

<MyComp paramOne='val1' paramTwo='val2' />

该类是:

import * as React from 'react';

export interface MyCompProps {
    paramOne: string;
    paramTwo: string;
}

export interface MyCompState {

}

export class MyComp extends React.Component<MyCompProps, MyCompState> {
    public render() {
        return <div>
            <ul>
                <li>paramOne: {this.props.paramOne}</li>
                <li>paramTwo: {this.props.paramTwo}</li>
            </ul>
        </div>;
    }
}

虽然可以在以下示例代码中访问val3

<MyComp paramOne='val1' paramTwo='val2'>val3</MyComp>
reactjs react-component tsx
1个回答
0
投票

您的val3可通过children属性访问。

export class MyComp extends React.Component<MyCompProps, MyCompState> {
    public render() {
        return <div>
            <ul>
                <li>paramOne: {this.props.paramOne}</li>
                <li>paramTwo: {this.props.paramTwo}</li>
                <li>children: {this.props.children}</li>
            </ul>
        </div>;
    }
}
<MyComp paramOne='val1' paramTwo='val2'>val3</MyComp>
© www.soinside.com 2019 - 2024. All rights reserved.