admin管理员组文章数量:1398790
For example, if I needed to make a "small" and a "large" version of a button that had a lot of (but not all) methods & state in mon, what would be the best way to implement this in React Native?
The issue I have with simply extending the "parent" ponent (I may be wrong about this) is that working with Props has been messy.
The way I've been implementing it has simply been to give the "parent" ponent a boolean prop (in this example, to represent "small" or "large") and then changing its features based on that boolean's value. The "child" ponents are pretty much only there for readability.
For example, if I needed to make a "small" and a "large" version of a button that had a lot of (but not all) methods & state in mon, what would be the best way to implement this in React Native?
The issue I have with simply extending the "parent" ponent (I may be wrong about this) is that working with Props has been messy.
The way I've been implementing it has simply been to give the "parent" ponent a boolean prop (in this example, to represent "small" or "large") and then changing its features based on that boolean's value. The "child" ponents are pretty much only there for readability.
Share Improve this question asked Nov 12, 2017 at 6:11 philphil 5376 silver badges20 bronze badges3 Answers
Reset to default 2Just extends your ponent to make a child ponent.
class Label extends React.Component{
constructor(props){
super(props);
this.className='plain-label';
}
render(){
return <span className={this.className}>
{this.props.children}
</span>
}
}
class SmallLabel extends Label{
constructor(props){
super(props);
this.className = this.className + ' small-label';
}
}
Then use it:
class Main extends React.Component{
render(){
....
<Label> Plain Label </Label>
<SmallLabel> SmallLabel </SmallLabel>
}
}
Inheritance is in most scenarios — just not good a viable solution. Because extending ponents with inheritance more or less leads to a scenario at some point, where the behaviors cannot be clubbed seamlessly. However, with Composition, its possible.
Good practice for extending/subclassing React Component, See: https://discuss.reactjs/t/best-practices-for-extending-subclassing-ponents/1820/3
If you are not going to override any methods and just going to make visual changes it is better to use props. Creating different ponents just for visual changes can create unnecessary plicated logic.
What you can do is to get use of defaultProps
Example
class CustomLabel extends React.Component{
render(){
const { isSmall, containerStyle, children } = this.props;
return (
<span
className={`plain-label ${(isSmall && 'small-label')}`}
style={containerStyle}
>
{children}
</span>
)
}
}
CustomLabel.defaultProps = {
isSmall: false,
containerStyle: {},
children: 'I need some children to display'
};
export default CustomLabel;
<CutomLabel>{'Plain label because gets the default props'}</CustomLabel>
<CustomLabel isSmall>{'Small label because prop is true'}</CustomLabel>
<CustomLabel isSmall containerStyle={{ color: 'red' }}>{'Red small label'}</CustomLabel>
As far as the sizing goes I would remend just passing the classname to the ponent, and having css styling conditional upon that classname (if you are using external stylesheets). As far as extending the class goes its hard to give an accurate answer without knowing your exact use-case.
本文标签: javascriptBest way to 39inherit39 from other components in React NativeStack Overflow
版权声明:本文标题:javascript - Best way to 'inherit' from other components in React Native? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744625853a2616253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论