是否可以访问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>
您的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>