一姨伟、 {...this.props}之惑
接觸rn也有段時間了碰辅,有{...this.props}
這么個東東老是出現在眼前掸宛,但終究不知道是干什么用的弟断,今天就來記錄一下创坞。
...this.props介紹
props
是RN中的屬性婆廊,由父組件指定极颓,子組件接收湿痢。至于...this.props
則是props
提供的語法糖霍骄,可以將父組件中的屬性賦值給子組件(我的理解就是變相的繼承咯)台囱。
用法
假設我們要定義一個打招呼的組件
定義組件
export default class SayHello extends Component {
constructor(props) {
super(props);
}
render() {
return(
<Text {...this.props}>Hello {this.props.name}</Text>
);
};
}
調用組件
render() {
return(
<SayHello
name = 'World'
suppressHighlighting = {true} />
);
};
注意:suppressHighlighting
是父組件的屬性,SayHello
組件只定義了name
一個屬性