? react Композиция против наследования
Primary tabs
Родитель:
import React from 'react'; import jswl from 'js-wrapper-lib'; class Input extends React.Component { constructor(props) { super(props); this.state = { name: jswl.isEmpty(this.props.name) ? 'noname' : this.props.name, className: jswl.isEmpty(this.props.className) ? 'uk-input uk-border-pill' : this.props.className, required: jswl.isEmpty(this.props.required) ? false : true, placeholder: jswl.isEmpty(this.props.placeholder) ? '' : true, type: jswl.isEmpty(this.props.type) ? 'text' : this.props.required, value: jswl.isEmpty(this.props.value) ? '' : this.props.required, onChange: jswl.isEmpty(this.props.onChange) ? () => { } : this.props.onChange, }; } render() { return ( <input name={this.state.name} className={this.state.className} required={this.state.required} placeholder={this.state.placeholder} type={this.state.type} value={this.state.value} onChange={this.state.onChange} /> ); } } export default Input;
Потомок:
import React from 'react'; import Input from 'Input'; class PasswordInput extends Input { render() { return ( <input name={this.state.name} className={this.state.className} required={this.state.required} placeholder="Password" type="password" value={this.state.value} onChange={this.state.onChange} /> ); } } export default PasswordInput;
- Log in to post comments
- 1217 reads